Exemplo n.º 1
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit()
     HfFolder.delete_token()
     self._api.logout(token)
     print("Successfully logged out.")
Exemplo n.º 2
0
    def run(self):
        token = HfFolder.get_token()
        if token is None:
            print("Not logged in")
            exit(1)
        local_path = os.path.abspath(self.args.path)
        if os.path.isdir(local_path):
            if self.args.filename is not None:
                raise ValueError(
                    "Cannot specify a filename override when uploading a folder."
                )
            rel_path = os.path.basename(local_path)
            files = self.walk_dir(rel_path)
        elif os.path.isfile(local_path):
            filename = self.args.filename if self.args.filename is not None else os.path.basename(
                local_path)
            files = [(local_path, filename)]
        else:
            raise ValueError(
                "Not a valid file or directory: {}".format(local_path))

        if sys.platform == "win32":
            files = [(filepath, filename.replace(os.sep, "/"))
                     for filepath, filename in files]

        if len(files) > UPLOAD_MAX_FILES:
            print(
                "About to upload {} files to S3. This is probably wrong. Please filter files before uploading."
                .format(ANSI.bold(len(files))))
            exit(1)

        user, _ = self._api.whoami(token)
        namespace = self.args.organization if self.args.organization is not None else user

        for filepath, filename in files:
            print(
                "About to upload file {} to S3 under filename {} and namespace {}"
                .format(ANSI.bold(filepath), ANSI.bold(filename),
                        ANSI.bold(namespace)))

        choice = input("Proceed? [Y/n] ").lower()
        if not (choice == "" or choice == "y" or choice == "yes"):
            print("Abort")
            exit()
        print(
            ANSI.bold(
                "Uploading... This might take a while if files are large"))
        for filepath, filename in files:
            try:
                access_url = self._api.presign_and_upload(
                    token=token,
                    filename=filename,
                    filepath=filepath,
                    organization=self.args.organization)
            except HTTPError as e:
                print(e)
                print(ANSI.red(e.response.text))
                exit(1)
            print("Your file now lives at:")
            print(access_url)
Exemplo n.º 3
0
    def run(self):
        token = HfFolder.get_token()
        if token is None:
            print("Not logged in")
            exit(1)
        local_path = os.path.abspath(self.args.path)
        if os.path.isdir(local_path):
            if self.args.filename is not None:
                raise ValueError("Cannot specify a filename override when uploading a folder.")
            rel_path = os.path.basename(local_path)
            files = self.walk_dir(rel_path)
        elif os.path.isfile(local_path):
            filename = self.args.filename if self.args.filename is not None else os.path.basename(local_path)
            files = [(local_path, filename)]
        else:
            raise ValueError("Not a valid file or directory: {}".format(local_path))

        for filepath, filename in files:
            print("About to upload file {} to S3 under filename {}".format(ANSI.bold(filepath), ANSI.bold(filename)))

        choice = input("Proceed? [Y/n] ").lower()
        if not (choice == "" or choice == "y" or choice == "yes"):
            print("Abort")
            exit()
        print(ANSI.bold("Uploading... This might take a while if files are large"))
        for filepath, filename in files:
            access_url = self._api.presign_and_upload(token=token, filename=filename, filepath=filepath)
            print("Your file now lives at:")
            print(access_url)
Exemplo n.º 4
0
    def run(self):
        token = HfFolder.get_token()
        if token is None:
            print("Not logged in")
            exit(1)
        local_path = os.path.abspath(self.args.path)
        if os.path.isdir(local_path):
            if self.args.filename is not None:
                raise ValueError("Cannot specify a filename override when uploading a folder.")
            rel_path = os.path.basename(local_path)
            files = self.walk_dir(rel_path)
        elif os.path.isfile(local_path):
            filename = self.args.filename if self.args.filename is not None else os.path.basename(local_path)
            files = [(local_path, filename)]
        else:
            raise ValueError("Not a valid file or directory: {}".format(local_path))

        if sys.platform == "win32":
            files = [(filepath, filename.replace(os.sep, "/")) for filepath, filename in files]

        if len(files) > UPLOAD_MAX_FILES:
            print(
                "About to upload {} files to S3. This is probably wrong. Please filter files before uploading.".format(
                    ANSI.bold(len(files))
                )
            )
            exit(1)
Exemplo n.º 5
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit()
     try:
         user = self._api.whoami(token)
         print(user)
     except HTTPError as e:
         print(e)
Exemplo n.º 6
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit(1)
     try:
         self._api.delete_obj(token, filename=self.args.filename)
     except HTTPError as e:
         print(e)
         exit(1)
     print("Done")
Exemplo n.º 7
0
 def test_token_workflow(self):
     """
     Test the whole token save/get/delete workflow,
     with the desired behavior with respect to non-existent tokens.
     """
     token = "token-{}".format(int(time.time()))
     HfFolder.save_token(token)
     self.assertEqual(HfFolder.get_token(), token)
     HfFolder.delete_token()
     HfFolder.delete_token()
     # ^^ not an error, we test that the
     # second call does not fail.
     self.assertEqual(HfFolder.get_token(), None)
Exemplo n.º 8
0
    def run(self):
        print("""
        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        """)
        username = input("Username: "******"Login successful")
        print("Your token:", token, "\n")
        print("Your token has been saved to", HfFolder.path_token)
Exemplo n.º 9
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit()
     try:
         user, orgs = self._api.whoami(token)
         print(user)
         if orgs:
             print(ANSI.bold("orgs: "), ",".join(orgs))
     except HTTPError as e:
         print(e)
Exemplo n.º 10
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit(1)
     try:
         self._api.delete_obj(token, filename=self.args.filename, organization=self.args.organization)
     except HTTPError as e:
         print(e)
         print(ANSI.red(e.response.text))
         exit(1)
     print("Done")
Exemplo n.º 11
0
    def run(self):
        token = HfFolder.get_token()
        if token is None:
            print("Not logged in")
            exit(1)
        try:
            stdout = subprocess.check_output(["git",
                                              "--version"]).decode("utf-8")
            print(ANSI.gray(stdout.strip()))
        except FileNotFoundError:
            print("Looks like you do not have git installed, please install.")

        try:
            stdout = subprocess.check_output(["git-lfs",
                                              "--version"]).decode("utf-8")
            print(ANSI.gray(stdout.strip()))
        except FileNotFoundError:
            print(
                ANSI.red(
                    "Looks like you do not have git-lfs installed, please install."
                    " You can install from https://git-lfs.github.com/."
                    " Then run `git lfs install` (you only have to do this once)."
                ))
        print("")

        user, _ = self._api.whoami(token)
        namespace = self.args.organization if self.args.organization is not None else user

        print("You are about to create {}".format(
            ANSI.bold(namespace + "/" + self.args.name)))

        if not self.args.yes:
            choice = input("Proceed? [Y/n] ").lower()
            if not (choice == "" or choice == "y" or choice == "yes"):
                print("Abort")
                exit()
        try:
            url = self._api.create_repo(token,
                                        name=self.args.name,
                                        organization=self.args.organization)
        except HTTPError as e:
            print(e)
            print(ANSI.red(e.response.text))
            exit(1)
        print("\nYour repo now lives at:")
        print("  {}".format(ANSI.bold(url)))
        print("\nYou can clone it locally with the command below,"
              " and commit/push as usual.")
        print(f"\n  git clone {url}")
        print("")
Exemplo n.º 12
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit(1)
     try:
         objs = self._api.list_objs(token)
     except HTTPError as e:
         print(e)
         exit(1)
     if len(objs) == 0:
         print("No shared file yet")
         exit()
     rows = [[obj.filename, obj.LastModified, obj.ETag, obj.Size] for obj in objs]
     print(self.tabulate(rows, headers=["Filename", "LastModified", "ETag", "Size"]))
Exemplo n.º 13
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit(1)
     try:
         objs = self._api.list_repos_objs(token, organization=self.args.organization)
     except HTTPError as e:
         print(e)
         print(ANSI.red(e.response.text))
         exit(1)
     if len(objs) == 0:
         print("No shared file yet")
         exit()
     rows = [[obj.filename, obj.lastModified, obj.commit, obj.size] for obj in objs]
     print(tabulate(rows, headers=["Filename", "LastModified", "Commit-Sha", "Size"]))
Exemplo n.º 14
0
    def run(self):
        token = HfFolder.get_token()
        if token is None:
            print("Not logged in")
            exit(1)
        filepath = os.path.join(os.getcwd(), self.args.file)
        filename = self.args.filename if self.args.filename is not None else os.path.basename(
            filepath)
        print("About to upload file {} to S3 under filename {}".format(
            ANSI.bold(filepath), ANSI.bold(filename)))

        choice = input("Proceed? [Y/n] ").lower()
        if not (choice == "" or choice == "y" or choice == "yes"):
            print("Abort")
            exit()
        print(
            ANSI.bold("Uploading... This might take a while if file is large"))
        access_url = self._api.presign_and_upload(token=token,
                                                  filename=filename,
                                                  filepath=filepath)
        print("Your file now lives at:")
        print(access_url)
Exemplo n.º 15
0
 def run(self):
     token = HfFolder.get_token()
     if token is None:
         print("Not logged in")
         exit()
     try: