[TuT] Python Scripts kompilieren

05/07/2013 01:46 Pacifista'#1
Vorab: Ich weiß nicht ob es so einen Thread schon gibt, falls doch, bitte löschen.

Important: Falls Ihr *.py Dateien aus dem Metin2 Client verwendet solltet ihr aufjedenfall Python 2.2 installieren und verwenden!

Hallo Community,

ich dachte mir ich zeige euch heute mal wie Ihr eure Python Scripts kompilieren könnt. (In unserem Fall einfach mal eine *.py Datei in eurem Metin2 Client)

Für das Tutorial benötigt Ihr folgendes:
  • Python (ich verwende Python 3.3.1)
  • Einzelne *.py Scripts
Zum Tutorial:

Ihr installiert euch wie oben erwähnt Python, geht in den Pfad indem Ihr es installiert habt und legt am besten einen neuen Ordner an. (z.B 'my_own_scripts')
Dort legt Ihr nun alle *.py Dateien rein, die Ihr kompiliert haben wollt.

Nun geht Ihr zurück in das (in meinem Falle) Python33 Verzeichnis und startet von dort aus die 'Python.exe'.

Nun öffnet sich eine Konsole, in der Ihr folgendes eintragt:
Code:
Falls Ihr compileall verwenden wollt:

import compileall

Falls Ihr py_compile verwenden wollt:

import py_compile
Erklärung:
Dieser Befehl importiert 'compileall.py' oder 'py_compile' und ermöglicht es uns Befehle aus diesem Script zu verwenden.

Danach kommt dieser Befehl:
Code:
Falls Ihr compileall verwenden wollt:

compileall.compile_dir('my_own_scripts', force=True)

Falls Ihr py_compile verwenden wollt:

py_compile.compile("my_own_scripts\dateiname.py")
Erklärung:
COMPILEALL: Dieser Befehl listet alle *.py Dateien im Ordner 'my_own_scripts' im Python33 Verzeichnis auf und kompiliert Sie im Pfad: 'my_own_scripts\__pycache__\'.
PY_COMPILE: Dieser Befehl kompiliert eine einzelne *.py Datei im ausgewählten Verzeichnis.

Falls Ihr dass erledigt habt wird in eurem Ordner 'my_own_scripts' ein neuer Ordner namens '__pycache__' angelegt, indem sich nun die kompilierten nicht mehr *.py sondern *.pyc (Python Compiled) Dateien befinden, diese können falls Ihr Python 2.2 verwendet habt, normal in eurem Client weiterverwendet werden und halten zumindest Kinder von dem Inhalt eurer Python Scripts ab.

Ich hoffe ich konnte euch helfen.

#Added Python 2.2, VT bitte selbst ausführen.
05/07/2013 01:55 .XXShuzZzle#2
Du weißt schon wenn du das mit Py 3.3 Kompilierst,dass Metin2 das nicht lesen kann weil Py 2.2 das anders kompiliert? Außerdem würde ich py_compile benutzen. Ich kann keinen Zusammenhang zu Metin2 hier erkennen.
05/07/2013 01:56 Pacifista'#3
Habe Metin2 doch nur als Beispiel genommen. ._.
#Hab noch mal 'ne Anmerkung dazu geschrieben, zufrieden? :3
05/07/2013 02:05 Analyze™#4
mit compilieren von .py keine ahnungx xD

wo bekomm ich py dingens für 2.2 her?

grüße
05/07/2013 02:22 Pacifista'#5
Einfach mal in Google nach 'Python GetIt' suchen, da ich nicht weiß ob ich hier einen Link posten darf. :awesome:
05/07/2013 03:29 Analyze™#6
Quote:
Originally Posted by Pacifista' View Post
Einfach mal in Google nach 'Python GetIt' suchen, da ich nicht weiß ob ich hier einen Link posten darf. :awesome:
so dann packst noch decompiling mit rein in den startpost und dann ist es komplett :)
05/07/2013 03:35 Pacifista'#7
Geh kacken. :awesome:

Soll zum Schutz und nicht zur Zerstörung dienen. xD
05/07/2013 06:28 lollo_9_1#8
This "tutorial" is partially/quite wrong.
  1. Metin2 uses python2.2.1, u cannot import a pyc module compiled in other python version (only with python2.2.*, python2.2.2/2.2.3 allowed)
  2. Every .py, when imported, generates the relative .pyc (or .pyo if u start python.exe with -O option) in byte-code and runned.
  3. Therefore, in agreement with the above points, u can create a relative pyc/pyo file simply importing them
  4. Without a program of dubious origin:
    • Download python2.2 [Only registered and activated users can see links. Click Here To Register...] (official website)
    • Start python.exe (with -O option if u want a pyo file instead of a pyc, -O=optimization) and:
      Code:
      #first way A (compile and import XXX.py file)
      try:
      	import yourmodule #create a .pyc/o file from .py if .pyc/o file not exists
      except ImportError:
      	pass
      #first way B (compile and import XXX.py file)
      import __builtin__
      try:
      	__builtin.__import__("yourmodule") #create a .pyc/o file from .py if .pyc/o file not exists
      except ImportError:
      	pass
      #second way (all recursive py files in XXX directory and subdirectories)
      import compileall
      compileall.compile_dir("lib/", force=1) #in python2.2 bool not exists, metin uses TRUE and FALSE constants with the relative values 1 and 0, in python u can use a / instead of \ to indicate paths
      #third way (compile XXX file)
      import py_compile
      py_compile.compile("yourmodule.py")
      [Only registered and activated users can see links. Click Here To Register...]
      [Only registered and activated users can see links. Click Here To Register...]
      [Only registered and activated users can see links. Click Here To Register...]
Now, to load in client (ymir has rewritten __import__ function) ur pyc file u must use marshal module like this:
Code:
# system.py
import marshal
marshal.loads(pack_file("yourmodule.pyc",'rb').read()[8:])#first 8 byte=magic
#newmodule = _process_result(marshal.loads(pack_file(filename,'rb').read()[8:]),name)
05/07/2013 07:19 Pacifista'#9
Thanks for the help, I'm sorry if I described something wrong.
I have only taken Metin2 as an example.

Sorry for my bad english. :awesome:
05/07/2013 07:32 JachuPL#10
and how to uncompile compiled scripts (if that's possible)?
05/07/2013 08:27 Pacifista'#11
Yes, it's possible.
05/07/2013 09:08 xP3NG3Rx#12
Google>python22 decompyle/decompile
Or here is my upload: [Only registered and activated users can see links. Click Here To Register...]

How2 setup: Use your brain.

Example for decompile:
Code:
from decompyle import decompyle_file as dec
s=open('decompiled.py','w')
dec('compiledfile.pyc', s,0,0)
05/07/2013 09:09 Prince43™#13
Quote:
Originally Posted by .XXShuzZzle View Post
Du weißt schon wenn du das mit Py 3.3 Kompilierst,dass Metin2 das nicht lesen kann weil Py 2.2 das anders kompiliert? Außerdem würde ich py_compile benutzen. Ich kann keinen Zusammenhang zu Metin2 hier erkennen.

Mein Hasi, in deinem satz steht schon die Lösung :) Nachdenken----Komplieren = Lösung <3

B2T: Schöne sache, jedoch gibt es da noch paar haaken.


Kind Regards

Prince43™
05/07/2013 11:17 xNe0nx3#14
Und was ist mit mir ich hab dir geholfen ;D
05/07/2013 12:03 .XXShuzZzle#15
Quote:
Originally Posted by JachuPL View Post
and how to uncompile compiled scripts (if that's possible)?
yes but not to 100% because it is still byte code