コード例 #1
0
    def __init__(self) -> None:
        """Load configuration or initialize on instance creation"""
        self.overleaf_config_file = os.path.join(
            Gigantum.get_gigantum_directory(), 'overleaf.json')
        self.overleaf_repo_directory = os.path.join(
            Gigantum.get_overleaf_root_directory(), 'project')
        self.overleaf_credential_file = os.path.join(
            Gigantum.get_overleaf_root_directory(), 'credentials.json')

        self.config: OverleafConfig = self._load_config()

        # Clone the Overleaf git repo if needed
        if os.path.isfile(self.overleaf_config_file):
            if not os.path.isdir(self.overleaf_repo_directory):
                # Overleaf project does not exist locally yet, clone
                self._clone()
コード例 #2
0
    def _set_creds(self, email: str, password: str) -> None:
        """ Write credentials to an untracked file

        Args:
            email: the Overleaf email to save
            password: the Overleaf password to save

        Returns:
            None
        """
        if not os.path.isdir(Gigantum.get_overleaf_root_directory()):
            os.makedirs(Gigantum.get_overleaf_root_directory())

        creds = {"email": email, "password": password}

        with open(self.overleaf_credential_file, 'wt') as cf:
            json.dump(creds, cf)
コード例 #3
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_unlink_image(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True

        gigaleaf.unlink_image('../output/fig1.png')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is False

        gigaleaf.sync()

        # Delete everything in untracked, reinit, and should still not see the files
        shutil.rmtree(gigaleaf.overleaf.overleaf_repo_directory)
        gigaleaf = None

        gigaleaf = Gigaleaf()
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is False
コード例 #4
0
    def test_link_csv_file_and_sync(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata',
                    'table_pkl.json').is_file() is False

        gigaleaf.link_dataframe('../output/table.pkl',
                                to_latex_kwargs={
                                    "index": False,
                                    "caption": "My table"
                                })

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'table_pkl.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'subfiles', 'table_pkl.tex').is_file() is True

        # Delete everything in untracked, reinit, and should still see the files
        shutil.rmtree(gigaleaf.overleaf.overleaf_repo_directory)
        gigaleaf = None

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata',
                    'table_pkl.json').is_file() is False
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'subfiles', 'table_pkl.tex').is_file() is False
        gigaleaf = Gigaleaf()
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'table_pkl.json').is_file() is True
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'subfiles', 'table_pkl.tex').is_file() is True
コード例 #5
0
    def write_subfile(self) -> None:
        """Method to write the Latex subfile

        Returns:
            None
        """
        if not isinstance(self.metadata, DataframeFileMetadata):
            raise ValueError(
                f"Incorrect metadata type loaded: {type(self.metadata)}")

        if pandas is None:
            raise EnvironmentError(
                "Dataframe pickle file support requires pandas. "
                "Please run `pip install gigaleaf[pandas]`")

        subfile_template = Template(
            r"""\documentclass[../../main.tex]{subfiles}

% Subfile autogenerated by gigaleaf
% Gigantum revision: $gigantum_version
% Image content hash: $content_hash
\begin{document}

{$table}

\end{document}
""")

        with open(
                Path(Gigantum.get_project_root(),
                     self.metadata.gigantum_relative_path).absolute().as_posix(
                     ), 'rb') as f:
            df = pandas.read_pickle(f)
            table = df.to_latex(**self.metadata.to_latex_kwargs)

        filename = "gigantum/data/" + Path(
            self.metadata.gigantum_relative_path).name

        subfile_populated = subfile_template.substitute(
            filename=filename,
            gigantum_version=Gigantum.get_current_revision(),
            content_hash=self.metadata.content_hash,
            table=table)

        Path(self.subfile_filename).write_text(subfile_populated)
コード例 #6
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_link_image_and_sync(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png',
                            caption="My figure",
                            label='fig111',
                            alignment='right',
                            width='0.3\\textwidth')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True

        # Delete everything in untracked, reinit, and should still see the files
        shutil.rmtree(gigaleaf.overleaf.overleaf_repo_directory)
        gigaleaf = None

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is False
        gigaleaf = Gigaleaf()
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True
        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True
コード例 #7
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_delete_project_when_empty(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(gigaleaf.overleaf.overleaf_config_file).is_file() is True

        gigaleaf.delete()

        assert Path(Gigantum.get_overleaf_root_directory()).is_dir() is False
        assert Path(gigaleaf.overleaf.overleaf_config_file).is_file() is False
コード例 #8
0
    def data_filename(self) -> str:
        """The absolute path to the linked file's data in the data directory

        The data directory is the location managed by gigaleaf where linked file contents are stored

        Returns:
            absolute path to the file
        """
        overleaf_gigantum_path = Path(Gigantum.get_overleaf_root_directory(), 'project', 'gigantum', 'data')
        return Path(overleaf_gigantum_path, Path(self.metadata.gigantum_relative_path).name).absolute().as_posix()
コード例 #9
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_delete_project_link(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True

        gigaleaf.delete()

        assert Path(Gigantum.get_overleaf_root_directory()).is_dir() is False
        assert Path(gigaleaf.overleaf.overleaf_config_file).is_file() is False
コード例 #10
0
    def update(self) -> None:
        """Method to update the file contents, latex subfile, and metadata file.

        Returns:

        """
        if self._is_modified():
            if self._should_copy_file() is True:
                # Copy file if needed
                shutil.copyfile(Path(Gigantum.get_project_root(), self.metadata.gigantum_relative_path),
                                self.data_filename)

            # Update commit hash in metadata
            kwargs = {"content_hash": self._hash_file(Path(Gigantum.get_project_root(),
                                                           self.metadata.gigantum_relative_path).absolute().as_posix()),
                      "metadata_filename": self.metadata_filename}
            self.write_metadata(**kwargs)

            # Latex subfile
            self.write_subfile()
コード例 #11
0
    def _is_modified(self) -> bool:
        """Helper method to check if a file has been modified since the last time you ran .sync()

        Returns:
            true if the file has changed since the last time you ran .sync(), false if it has not
        """
        if self.metadata.content_hash != self._hash_file(Path(Gigantum.get_project_root(),
                                                         self.metadata.gigantum_relative_path).absolute().as_posix()):
            return True
        else:
            return False
コード例 #12
0
    def subfile_filename(self) -> str:
        """The absolute path to the Linked File's subfile in the subfiles directory

        The subfiles directory is the location managed by gigaleaf where all generated subfiles are stored

        Returns:
            absolute path to the subfile
        """
        filename = Path(self.metadata_filename).name
        filename = filename.replace('.json', '.tex')
        overleaf_gigantum_path = Path(Gigantum.get_overleaf_root_directory(), 'project', 'gigantum', 'subfiles')
        return Path(overleaf_gigantum_path, filename).absolute().as_posix()
コード例 #13
0
    def link(cls, relative_path: str, **kwargs: Dict[str, Any]) -> None:
        """Method to link a file output in a Gigantum Project to an Overleaf project

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_fig.png`
            **kwargs: args specific to each LinkedFile implementation

        Returns:
            None
        """
        file_path = Path(relative_path).resolve()
        if file_path.is_file() is False:
            # File provided does not exist
            raise ValueError(f"The file {file_path} does not exist. Provide a relative path from the working"
                             f"directory to your file. In Jupyter, the working directory is the directory containing "
                             f"your notebook.")

        metadata_filename = cls.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata', metadata_filename)

        if metadata_abs_filename.exists() is True:
            # This is an update to the link, so get the current content hash for the file.
            with open(metadata_abs_filename, 'rt') as mf:
                current_metadata: Dict[str, Any] = json.load(mf)
                content_hash = current_metadata['content_hash']
        else:
            # Set content hash to init so it is always detected as "modified" on first link
            content_hash = "init"

        full_kwargs = {
            "gigantum_relative_path": file_path.relative_to(Path(Gigantum.get_project_root()).resolve()).as_posix(),
            "gigantum_version": Gigantum.get_current_revision(),
            "classname": cls.__name__,
            "content_hash": content_hash,
            "metadata_filename": metadata_filename}
        full_kwargs.update(kwargs)

        cls.write_metadata(**full_kwargs)
コード例 #14
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_link_image_with_defaults(self, gigantum_project_fixture):
        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png')

        metadata_file = Path(Gigantum.get_overleaf_root_directory(), 'project',
                             'gigantum', 'metadata', 'fig1_png.json')
        assert metadata_file.is_file() is True

        with open(metadata_file, 'rt') as mf:
            data = json.load(mf)

        assert data['gigantum_relative_path'] == 'output/fig1.png'
        assert data['gigantum_version'] != 'init'
        assert len(data['gigantum_version']) == 40
        assert data['classname'] == 'ImageFile'
        assert data['content_hash'] == 'init'
        assert data['caption'] is None
        assert data['label'] == 'fig:fig1'
        assert data['width'] == '0.5\\textwidth'
        assert data['alignment'] == 'center'
コード例 #15
0
    def unlink_csv(self, relative_path: str) -> None:
        """Method to unlink a csv file from your Overleaf project.

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_table.csv`

        Returns:
            None
        """
        metadata_filename = ImageFile.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata',
                                     metadata_filename)
        csv_file = load_linked_file(metadata_abs_filename.as_posix())
        csv_file.unlink()
コード例 #16
0
    def _init_config(self) -> None:
        """Private method to configure an overleaf integration

        Returns:
            None
        """
        # Prompt for overleaf project URL
        intro_message = Path(
            Path(__file__).parent.absolute(), 'resources',
            'intro_message.txt').read_text()
        print(intro_message)

        project_url = input("Overleaf Git url: ").strip()

        # Handle if the user passed in the link or the whole git command that Overleaf displays
        idx = project_url.find('git.overleaf.com')
        if idx == -1:
            raise ValueError(
                "Overleaf Git URL is malformed. Should be like: https://git.overleaf.com/xxxxxxxxxxxxx"
            )
        else:
            project_url = 'https://' + project_url[idx:].split(maxsplit=1)[0]

        # Prompt for email and password
        self._init_creds()

        # Write overleaf config file
        config = {
            "overleaf_git_url": project_url,
            "gigaleaf_version": gigaleaf_version
        }
        with open(self.overleaf_config_file, 'wt') as cf:
            json.dump(config, cf)

        # Commit the config file
        Gigantum.commit_overleaf_config_file(self.overleaf_config_file)
コード例 #17
0
    def write_subfile(self) -> None:
        """Method to write the Latex subfile

        Returns:
            None
        """
        if not isinstance(self.metadata, CsvFileMetadata):
            raise ValueError(
                f"Incorrect metadata type loaded: {type(self.metadata)}")

        subfile_template = Template("""\documentclass[../../main.tex]{subfiles}

% Subfile autogenerated by gigaleaf
% Gigantum revision: $gigantum_version
% Image content hash: $content_hash
\\begin{document}

\\begin{table}[ht]
\\centering
\\csvautotabular[respect all]{$filename}
\\label{$label}
{$caption}
\\end{table}

\\end{document}
""")

        if self.metadata.caption:
            caption = f"\\caption{{{self.metadata.caption}}}"
        else:
            caption = "\n"

        filename = "gigantum/data/" + Path(
            self.metadata.gigantum_relative_path).name

        subfile_populated = subfile_template.substitute(
            filename=filename,
            gigantum_version=Gigantum.get_current_revision(),
            content_hash=self.metadata.content_hash,
            label=self.metadata.label,
            caption=caption)

        Path(self.subfile_filename).write_text(subfile_populated)
コード例 #18
0
    def write_subfile(self) -> None:
        """Method to write the Latex subfile

        Returns:
            None
        """
        if not isinstance(self.metadata, ImageFileMetadata):
            raise ValueError(
                f"Incorrect metadata type loaded: {type(self.metadata)}")

        subfile_template = Template("""\documentclass[../../main.tex]{subfiles}

% Subfile autogenerated by gigaleaf
% Gigantum revision: $gigantum_version
% Image content hash: $content_hash
\\begin{document}

\\begin{figure}[bh]
\\includegraphics[width=$width, $alignment]{$filename}
\\label{$label}
{$caption}
\\end{figure}

\\end{document}
""")

        if self.metadata.caption:
            caption = f"\\caption{{{self.metadata.caption}}}"
        else:
            caption = "\n"

        subfile_populated = subfile_template.substitute(
            filename=Path(self.metadata.gigantum_relative_path).stem,
            gigantum_version=Gigantum.get_current_revision(),
            content_hash=self.metadata.content_hash,
            width=self.metadata.width,
            alignment=self.metadata.alignment,
            caption=caption,
            label=self.metadata.label)

        Path(self.subfile_filename).write_text(subfile_populated)
コード例 #19
0
    def write_metadata(metadata_filename: str, **kwargs: Any) -> None:
        """Method to write metadata to disk

        Args:
            metadata_filename: name of the metadata file
            **kwargs:

        Returns:
            None
        """
        data = dict()
        if Path(metadata_filename).exists():
            # Update existing file
            with open(metadata_filename, 'rt') as mf:
                data = json.load(mf)

        data.update(kwargs)

        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata', metadata_filename)
        with open(metadata_abs_filename, 'wt') as mf:
            json.dump(data, mf)
コード例 #20
0
ファイル: test_gigaleaf.py プロジェクト: mtazzari/gigaleaf
    def test_update_image(self, gigantum_project_fixture):

        gigaleaf = Gigaleaf()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is False

        gigaleaf.link_image('../output/fig1.png', width='0.8\\textwidth')

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'metadata', 'fig1_png.json').is_file() is True

        gigaleaf.sync()

        assert Path(Gigantum.get_overleaf_root_directory(), 'project',
                    'gigantum', 'data', 'fig1.png').is_file() is True

        metadata_file = Path(Gigantum.get_overleaf_root_directory(), 'project',
                             'gigantum', 'metadata', 'fig1_png.json')
        with open(metadata_file, 'rt') as mf:
            data = json.load(mf)

        first_hash = data['content_hash']

        test_dir = Path(__file__).parent.absolute()
        shutil.copyfile(
            Path(test_dir, 'resources', 'fig1.png').as_posix(),
            Path(Gigantum.get_project_root(), 'output', 'fig1.png'))

        gigaleaf.sync()

        metadata_file = Path(Gigantum.get_overleaf_root_directory(), 'project',
                             'gigantum', 'metadata', 'fig1_png.json')
        with open(metadata_file, 'rt') as mf:
            data = json.load(mf)

        assert first_hash != data['content_hash']
コード例 #21
0
 def __init__(self) -> None:
     self.overleaf = Overleaf()
     self.gigantum = Gigantum(self.overleaf.overleaf_repo_directory)
コード例 #22
0
class Gigaleaf:
    """Class to link Gigantum Project outputs to an Overleaf Project"""
    def __init__(self) -> None:
        self.overleaf = Overleaf()
        self.gigantum = Gigantum(self.overleaf.overleaf_repo_directory)

    def link_image(self,
                   relative_path: str,
                   caption: Optional[str] = None,
                   label: Optional[str] = None,
                   width: str = "0.5\\textwidth",
                   alignment: str = 'center') -> None:
        """Method to link an image file to your Overleaf project for automatic updating

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_fig.png`
            caption: The caption for the figure in the auto-generated latex subfile
            label: The label for the figure in the auto-generated latex subfile
            width: A string setting the width of the figure for the figure in the auto-generated latex subfile
            alignment: A string setting the alignment for the figure in the auto-generated latex subfile. Supported
                       values are `left, right, center, inner, and outer`


        If this method is called more than once for a given `gigantum_relative_path`, the link will simply be updated.
        This is useful for doing things like editing a caption.


        Returns:
            None
        """
        if not label:
            safe_filename = ImageFile.get_safe_filename(relative_path)
            label = f"fig:{Path(safe_filename).stem}"

        kwargs: Dict[str, Any] = {
            "caption": caption,
            "label": label,
            "width": width,
            "alignment": alignment
        }

        ImageFile.link(relative_path, **kwargs)

    def unlink_image(self, relative_path: str) -> None:
        """Method to unlink an image file from your Overleaf project.

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_fig.png`

        Returns:
            None
        """
        metadata_filename = ImageFile.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata',
                                     metadata_filename)
        img_file = load_linked_file(metadata_abs_filename.as_posix())
        img_file.unlink()

    def link_csv(self,
                 relative_path: str,
                 caption: Optional[str] = None,
                 label: Optional[str] = None) -> None:
        """Method to link a csv file to your Overleaf project for automatic updating

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_table.csv`
            caption: The caption for the table in the auto-generated latex subfile
            label: The label for the table in the auto-generated latex subfile

        Returns:
            None
        """
        if not label:
            safe_filename = ImageFile.get_safe_filename(relative_path)
            label = f"table:{Path(safe_filename).stem}"

        kwargs: Dict[str, Any] = {"caption": caption, "label": label}

        CsvFile.link(relative_path, **kwargs)

    def unlink_csv(self, relative_path: str) -> None:
        """Method to unlink a csv file from your Overleaf project.

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_table.csv`

        Returns:
            None
        """
        metadata_filename = ImageFile.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata',
                                     metadata_filename)
        csv_file = load_linked_file(metadata_abs_filename.as_posix())
        csv_file.unlink()

    def link_dataframe(self, relative_path: str,
                       to_latex_kwargs: Dict[str, Any]) -> None:
        """Method to link a dataframe file to your Overleaf project for automatic updating

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_table.csv`
            to_latex_kwargs: a dictionary of key word arguments to pass into the pandas.DataFrame.to_latex method
                             (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_latex.html)

        Returns:
            None
        """
        # Clean kwargs sent to .to_latex()
        if 'buf' in to_latex_kwargs:
            del to_latex_kwargs['buf']

        kwargs = {"to_latex_kwargs": to_latex_kwargs}

        DataframeFile.link(relative_path, **kwargs)

    def unlink_dataframe(self, relative_path: str) -> None:
        """Method to unlink a dataframe file from your Overleaf project.

        Args:
            relative_path: relative path to the file from the current working dir, e.g. `../output/my_table.csv`

        Returns:
            None
        """
        metadata_filename = ImageFile.get_metadata_filename(relative_path)
        metadata_abs_filename = Path(Gigantum.get_overleaf_root_directory(),
                                     'project', 'gigantum', 'metadata',
                                     metadata_filename)
        dataframe_file = load_linked_file(metadata_abs_filename.as_posix())
        dataframe_file.unlink()

    def sync(self) -> None:
        """Method to synchronize your Gigantum and Overleaf projects.

        When you call this method, gigaleaf will do the following:

            * Pull changes from the Overleaf project
            * Check all linked files for changes. If changes exist it will update files in the Overleaf project
            * Commit changes to the Overleaf project
            * Push changes to the Overleaf project

        Returns:
            None
        """
        print("Syncing with Overleaf. Please wait...")
        self.overleaf.pull()

        linked_files = load_all_linked_files(
            self.overleaf.overleaf_repo_directory)
        for lf in linked_files:
            lf.update()

        self.overleaf.commit()

        self.overleaf.push()
        print("Sync complete.")

    def delete(self) -> None:
        """Removes the link between a Gigantum Project from an Overleaf Project

        Returns:
            None
        """
        gigaleaf_config_file = Path(self.overleaf.overleaf_config_file)
        if gigaleaf_config_file.is_file():
            print(
                "Removing integration from Overleaf and Gigantum projects. Please wait..."
            )
            self.overleaf.pull()

            gigantum_overleaf_dir = Path(self.overleaf.overleaf_repo_directory,
                                         'gigantum')
            if gigantum_overleaf_dir.is_dir():
                # Remove Gigantum dir from Overleaf Project if it exists (maybe you haven't synced yet)
                shutil.rmtree(gigantum_overleaf_dir.as_posix())

                # Commit and Push
                try:
                    self.overleaf.commit()
                    self.overleaf.push()
                except ValueError as err:
                    if "Your branch is up to date with 'origin/master'" not in str(
                            err):
                        # If you haven't synced yet, you'll get a git error because removing the dir doesn't actually
                        # change the repository state. If you get any other error, raise.
                        raise

            # Remove Overleaf Project dir and credentials from Gigantum Project
            overleaf_root_dir = Path(
                self.gigantum.get_overleaf_root_directory())
            if overleaf_root_dir.is_dir():
                shutil.rmtree(overleaf_root_dir.as_posix())

            # Remove gigaleaf config file from Gigantum Project & commit.
            gigaleaf_config_file.unlink()
            self.gigantum.commit_overleaf_config_file(
                gigaleaf_config_file.as_posix())
            print("Removal complete.")
        else:
            print(
                "gigaleaf has not been configured yet. Skipping removal process."
            )
コード例 #23
0
ファイル: fixtures.py プロジェクト: mtazzari/gigaleaf
def gigantum_project_fixture():
    unit_test_working_dir = os.path.join(tempfile.gettempdir(),
                                         uuid.uuid4().hex)
    os.makedirs(unit_test_working_dir)

    test_dir = pathlib.Path(__file__).parent.absolute()
    test_project_path = os.path.join(test_dir, 'resources',
                                     'example_project.zip')
    secret_file_path = os.path.join(test_dir, 'resources', 'secrets.json')
    with zipfile.ZipFile(test_project_path, 'r') as zip_ref:
        zip_ref.extractall(unit_test_working_dir)

    # Set the working dir to INSIDE the project
    unit_test_working_dir = os.path.join(unit_test_working_dir,
                                         'overleaf-test-project')

    with patch.object(Gigantum, "get_project_root") as patched_gigantum:
        patched_gigantum.return_value = unit_test_working_dir

        os.chdir(os.path.join(unit_test_working_dir, 'code'))

        with open(secret_file_path, 'rt') as sf:
            secrets = json.load(sf)

        # Configure overleaf config file and secrets
        config_file_path = os.path.join(unit_test_working_dir, '.gigantum',
                                        'overleaf.json')
        config = {
            "overleaf_git_url": secrets['git_url'],
            "gigaleaf_version": "0.1.0"
        }
        with open(config_file_path, 'wt') as cf:
            json.dump(config, cf)

        Gigantum.commit_overleaf_config_file(config_file_path)

        # Write credential file
        creds = {"email": secrets['email'], "password": secrets['password']}
        overleaf_dir = os.path.join(unit_test_working_dir,
                                    'output/untracked/overleaf')
        os.makedirs(overleaf_dir)
        with open(os.path.join(overleaf_dir, 'credentials.json'), 'wt') as cf:
            json.dump(creds, cf)

        # Yield and run test
        yield unit_test_working_dir

    # Clean up overleaf if it was set up
    overleaf_project_dir = os.path.join(overleaf_dir, 'project')
    gigantum_overleaf_dir = os.path.join(overleaf_project_dir, 'gigantum')
    if os.path.isdir(gigantum_overleaf_dir):
        # Remove gigantum dir in the project IN overleaf
        shutil.rmtree(gigantum_overleaf_dir)
        git_status = call_subprocess(['git', 'status'],
                                     overleaf_project_dir,
                                     check=True)
        if "nothing to commit, working tree clean" not in git_status:
            call_subprocess(['git', 'add', '-A'],
                            overleaf_project_dir,
                            check=True)
            call_subprocess(
                ['git', 'commit', '-m', 'Cleaning up integration test'],
                overleaf_project_dir,
                check=True)

            call_subprocess(['git', 'push'], overleaf_project_dir, check=True)

    # Clean up test project
    shutil.rmtree(unit_test_working_dir)