Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I add user submited files in the virtual filesystem? #198

Open
Darker opened this issue Nov 22, 2016 · 2 comments
Open

Can I add user submited files in the virtual filesystem? #198

Darker opened this issue Nov 22, 2016 · 2 comments

Comments

@Darker
Copy link

Darker commented Nov 22, 2016

I need to fetch files from user (using JS), then add those files in PyPyjs system so they can be imported. I am new to python and I found no docs for PyPy, could you please help me out?

I fetch the files as strings, so basically the question is how to make a file from string in pypy vm.

My complete plan is to run AI tournament without uploading user's code on server. The server will only send game field and ask AI to play (make valid move).

@albertjan
Copy link
Contributor

Have a look at https://github.com/pypyjs/pypyjs/blob/master/src/pypyjs.js#L539 esp line 564 😄

@Darker
Copy link
Author

Darker commented Nov 22, 2016

I am working on it and I already figured something out. I did not know about exec, so I did this:

      function addFileToPyPy(name, contents) {
          var encoded_document = btoa(unescape(encodeURIComponent(contents)));
          //console.log("Encoded data: ", encoded_document);
          var pycode = "with open(\""+name+"\", \"w\") as text_file:"
                     + "    text_file.write(base64.standard_b64decode(\""+encoded_document+"\"))";
          pypy_stdin(pycode);
      }

It seems to work, but I failed to import any of the files. I verified their existence using this code:

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir("./") if isfile(join("./", f))]
print(onlyfiles)

That code did output list of files that I have created:

['backup.py', 'board.py', 'border_player.py', 'game_board.py', 'headless_reversi_creator.py', 'player.py', 'predictive_player.py', 'random_player.py', 'README.txt', 'reversi_creator.py', 'reversi_view.py']

But import player fails:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named player

Another way I tested availability of file was execfile("player.py"):

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "player.py", line 1, in <module>
    from board import Board
ImportError: No module named board

(player.py imports board)

I'll try to use the direct method this.Module.FS_createDataFile, but I inspected pypyjs object in DOM and I see no Module property in the object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants