Skip to content

Instantly share code, notes, and snippets.

@bhatti
Forked from simonw/recover_source_code.md
Created March 12, 2017 01:37
Show Gist options
  • Save bhatti/04bb5cb56200e76a91c851dd1bc269c7 to your computer and use it in GitHub Desktop.
Save bhatti/04bb5cb56200e76a91c851dd1bc269c7 to your computer and use it in GitHub Desktop.

Revisions

  1. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # How to recover lost Python source code if it's still resident in-memory

    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6
    I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

    ## Attach a shell to the docker container

  2. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -8,11 +8,11 @@ I screwed up with git and managed to delete the code I had just written... but i

    apt-get update && apt-get install gdb

    ## Install pyrasite - this well let you attach a Python shell to the still-running process.
    ## Install pyrasite - this will let you attach a Python shell to the still-running process

    pip install pyrasite

    ## Install uncompyle6, which will let you get Python source code back from in-memory code objects:
    ## Install uncompyle6, which will let you get Python source code back from in-memory code objects

    pip install uncompyle6

    @@ -24,16 +24,16 @@ I screwed up with git and managed to delete the code I had just written... but i

    pyrasite-shell <PID>

    ## Now you're in an interactive prompt! Import the code you need to recover:
    ## Now you're in an interactive prompt! Import the code you need to recover

    >>> from my_package import my_module

    ## Figure out which functions and classes you need to recover:
    ## Figure out which functions and classes you need to recover

    >>> dir(my_module)
    ['MyClass', 'my_function']

    ## Decompile the function into source code:
    ## Decompile the function into source code

    >>> import uncompyle6
    >>> import sys
    @@ -44,10 +44,10 @@ I screwed up with git and managed to delete the code I had just written... but i
    # Python bytecode 2.7
    # Decompiled from: Python 2.7.12 (default, Nov 19 2016, 06:48:10)
    # [GCC 5.4.0 20160609]
    # Embedded file name: /srv/destination_service/destination_service/wsgi.py
    # Embedded file name: /srv/my_package/my_module.py
    function_body = "appears here"

    ## For the class, you'll need to decompile each method in turn:
    ## For the class, you'll need to decompile each method in turn

    >>> uncompyle6.main.uncompyle(
    2.7, my_module.MyClass.my_method.im_func.func_code, sys.stdout
  3. Simon Willison revised this gist Mar 11, 2017. No changes.
  4. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How to recover Python source code if it's still in-memory
    # How to recover lost Python source code if it's still resident in-memory

    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

  5. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 12 additions and 10 deletions.
    22 changes: 12 additions & 10 deletions recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,39 @@
    # How to recover Python source code if it's still in-memory

    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

    = Attach a shell to the docker container =
    ## Attach a shell to the docker container

    2. Install GDB (needed by pyrasite)
    ## Install GDB (needed by pyrasite)

    apt-get update && apt-get install gdb

    3. Install pyrasite - this well let you attach a Python shell to the still-running process.
    ## Install pyrasite - this well let you attach a Python shell to the still-running process.

    pip install pyrasite

    4. Install uncompyle6, which will let you get Python source code back from in-memory code objects:
    ## Install uncompyle6, which will let you get Python source code back from in-memory code objects:

    pip install uncompyle6

    5. Find the PID of the process that is still running
    ## Find the PID of the process that is still running

    ps aux | grep python

    6. Attach an interactive prompt using pyrasite
    ## Attach an interactive prompt using pyrasite

    pyrasite-shell <PID>

    7. Now you're in an interactive prompt! Import the code you need to recover:
    ## Now you're in an interactive prompt! Import the code you need to recover:

    >>> from my_package import my_module

    8. Figure out which functions and classes you need to recover:
    ## Figure out which functions and classes you need to recover:

    >>> dir(my_module)
    ['MyClass', 'my_function']

    9. Decompile the function into source code:
    ## Decompile the function into source code:

    >>> import uncompyle6
    >>> import sys
    @@ -45,7 +47,7 @@ I screwed up with git and managed to delete the code I had just written... but i
    # Embedded file name: /srv/destination_service/destination_service/wsgi.py
    function_body = "appears here"

    10. For the class, you'll need to decompile each method in turn:
    ## For the class, you'll need to decompile each method in turn:

    >>> uncompyle6.main.uncompyle(
    2.7, my_module.MyClass.my_method.im_func.func_code, sys.stdout
  6. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

    = Attach a shell to the docker container
    = Attach a shell to the docker container =

    2. Install GDB (needed by pyrasite)

  7. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

    1. Attach a shell to the docker container
    = Attach a shell to the docker container

    2. Install GDB (needed by pyrasite)

  8. Simon Willison revised this gist Mar 11, 2017. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,37 @@
    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back.
    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

    Attach a shell to the docker container
    1. Attach a shell to the docker container

    Install GDB (needed by pyrasite)
    2. Install GDB (needed by pyrasite)

    apt-get update && apt-get install gdb

    Install pyrasite - this well let you attach a Python shell to the still-running process.
    3. Install pyrasite - this well let you attach a Python shell to the still-running process.

    pip install pyrasite

    Install uncompyle2, which will let you get Python source code back from in-memory code objects:
    4. Install uncompyle6, which will let you get Python source code back from in-memory code objects:

    pip install uncompyle2
    pip install uncompyle6

    Find the PID of the process that is still running
    5. Find the PID of the process that is still running

    ps aux | grep python

    Attach an interactive prompt using pyrasite
    6. Attach an interactive prompt using pyrasite

    pyrasite-shell <PID>

    Now you're in an interactive prompt! Import the code you need to recover:
    7. Now you're in an interactive prompt! Import the code you need to recover:

    >>> from my_package import my_module
    Figure out which functions and classes you need to recover:
    8. Figure out which functions and classes you need to recover:

    >>> dir(my_module)
    ['MyClass', 'my_function']

    Decompile the function into source code:
    9. Decompile the function into source code:

    >>> import uncompyle6
    >>> import sys
    @@ -45,7 +45,7 @@ Decompile the function into source code:
    # Embedded file name: /srv/destination_service/destination_service/wsgi.py
    function_body = "appears here"

    For the class, you'll need to decompile each method in turn:
    10. For the class, you'll need to decompile each method in turn:

    >>> uncompyle6.main.uncompyle(
    2.7, my_module.MyClass.my_method.im_func.func_code, sys.stdout
  9. Simon Willison created this gist Mar 11, 2017.
    58 changes: 58 additions & 0 deletions recover_source_code.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    I screwed up with git and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back.

    Attach a shell to the docker container

    Install GDB (needed by pyrasite)

    apt-get update && apt-get install gdb

    Install pyrasite - this well let you attach a Python shell to the still-running process.

    pip install pyrasite

    Install uncompyle2, which will let you get Python source code back from in-memory code objects:

    pip install uncompyle2

    Find the PID of the process that is still running

    ps aux | grep python

    Attach an interactive prompt using pyrasite

    pyrasite-shell <PID>

    Now you're in an interactive prompt! Import the code you need to recover:

    >>> from my_package import my_module

    Figure out which functions and classes you need to recover:

    >>> dir(my_module)
    ['MyClass', 'my_function']

    Decompile the function into source code:

    >>> import uncompyle6
    >>> import sys
    >>> uncompyle6.main.uncompyle(
    2.7, my_module.my_function.func_code, sys.stdout
    )
    # uncompyle6 version 2.9.10
    # Python bytecode 2.7
    # Decompiled from: Python 2.7.12 (default, Nov 19 2016, 06:48:10)
    # [GCC 5.4.0 20160609]
    # Embedded file name: /srv/destination_service/destination_service/wsgi.py
    function_body = "appears here"

    For the class, you'll need to decompile each method in turn:

    >>> uncompyle6.main.uncompyle(
    2.7, my_module.MyClass.my_method.im_func.func_code, sys.stdout
    )
    # uncompyle6 version 2.9.10
    # Python bytecode 2.7
    # Decompiled from: Python 2.7.12 (default, Nov 19 2016, 06:48:10)
    # [GCC 5.4.0 20160609]
    # Embedded file name: /srv/my_package/my_module.py
    class_method_body = "appears here"