We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Run system management command not in queue
### Subject of the issue

Do not use the rqworker for the `python manage.py system` command. It runs a single git pull/clone, copies a single and imports a single csv file. This can be done without the rqworker.

This is an issue, when starting the web container, which runs  `python manage.py system` before the queue and the rqworker might be ready.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `ddionrails/imports/manager.py`
Content:
```
1 import logging
2 import os
3 import re
4 import shutil
5 from collections import OrderedDict
6 from pathlib import Path
7 
8 import django_rq
9 from django.conf import settings
10 
11 from config.helpers import script_list, script_list_output
12 from ddionrails.concepts.imports import (
13     AnalysisUnitImport,
14     ConceptImport,
15     ConceptualDatasetImport,
16     PeriodImport,
17     TopicImport,
18     TopicJsonImport,
19 )
20 from ddionrails.data.imports import (
21     DatasetImport,
22     DatasetJsonImport,
23     TransformationImport,
24     VariableImport,
25 )
26 from ddionrails.instruments.imports import (
27     ConceptQuestionImport,
28     InstrumentImport,
29     QuestionVariableImport,
30 )
31 from ddionrails.publications.imports import AttachmentImport, PublicationImport
32 from ddionrails.studies.imports import StudyDescriptionImport, StudyImport
33 from ddionrails.studies.models import Study
34 
35 logging.config.fileConfig("logging.conf")
36 logger = logging.getLogger(__name__)
37 
38 
39 class Repository:
40     def __init__(self, study_or_system):
41         self.study_or_system = study_or_system
42         self.link = study_or_system.repo_url()
43         self.name = study_or_system.name
44         self.path = os.path.join(settings.IMPORT_REPO_PATH, self.name)
45 
46     def clone_repo(self):
47         script = [
48             f"cd {settings.IMPORT_REPO_PATH}",
49             f"git clone --depth 1 {self.link} {self.name} -b {settings.IMPORT_BRANCH}",
50             f"cd {self.name}",
51             f"git checkout -b {settings.IMPORT_BRANCH} origin/{settings.IMPORT_BRANCH}",
52             f"git branch --set-upstream-to=origin/{settings.IMPORT_BRANCH} {settings.IMPORT_BRANCH}",
53         ]
54         script_list(script)
55 
56     def set_branch(self, branch=settings.IMPORT_BRANCH):
57         script = ["cd %s" % self.path, "git checkout %s" % branch]
58         script_list(script)
59 
60     def update_repo(self):
61         script = ["cd %s" % self.path, "git pull"]
62         script_list(script)
63 
64     def update_or_clone_repo(self):
65         print("[INFO] Repository path: %s" % self.path)
66         if os.path.isdir(self.path):
67             self.update_repo()
68         else:
69             self.clone_repo()
70 
71     def get_commit_id(self):
72         script = ["cd %s" % self.path, "git log --pretty=format:'%H' -n 1"]
73         return script_list_output(script)
74 
75     def set_commit_id(self):
76         study_or_system = self.study_or_system
77         study_or_system.current_commit = self.get_commit_id()
78         study_or_system.save()
79 
80     def import_required(self):
81         return self.study_or_system.current_commit != self.get_commit_id()
82 
83     def list_changed_files(self):
84         script = [
85             "cd %s" % self.path,
86             "git diff --name-only %s -- %s"
87             % (self.study_or_system.current_commit, settings.IMPORT_SUB_DIRECTORY),
88         ]
89         return script_list_output(script).split()
90 
91     def list_all_files(self):
92         script = ["cd %s" % self.path, "find %s" % settings.IMPORT_SUB_DIRECTORY]
93         return script_list_output(script).split()
94 
95     def import_list(self, import_all=False):
96         if import_all:
97             import_files = self.list_all_files()
98         else:
99             import_files = self.list_changed_files()
100         return import_files
101 
102 
103 class ImportLink:
104     def __init__(self, expression, importer, activate_import=True):
105 
106         print(expression)
107         print(importer)
108         print(activate_import)
109 
110         self.expression = re.compile(expression)
111         self.importer = importer
112         self.activate_import = activate_import
113 
114     def run_import(self, import_files, study=None):
115         if self.activate_import:
116             for import_file in import_files:
117                 self._process_import_file(import_file, study=study)
118 
119     def _import(self, study, import_file):
120         self.importer.run_import(import_file, study=study)
121 
122     def _process_import_file(self, import_file, study=None):
123         # import_file = import_file.replace(settings.IMPORT_SUB_DIRECTORY, "")
124         if self._match(import_file):
125             django_rq.enqueue(self._import, study, import_file)
126 
127     def _match(self, import_file):
128         return True if self.expression.match(import_file) else False
129 
130 
131 class SystemImportManager:
132     """Import the files from the system repository."""
133 
134     def __init__(self, system):
135         self.system = system
136         self.repo = Repository(system)
137 
138     def run_import(self):
139         """
140         Run the system import.
141         """
142 
143         base_dir = Path(
144             f"{settings.IMPORT_REPO_PATH}{self.system.name}/{settings.IMPORT_SUB_DIRECTORY}"
145         )
146         studies_file = base_dir / "studies.csv"
147         django_rq.enqueue(StudyImport.run_import, studies_file, self.system)
148 
149         # Copy background image to static/
150         image_file = base_dir / "background.png"
151         shutil.copy(image_file, "static/")
152         self.repo.set_commit_id()
153 
154 
155 class StudyImportManager:
156     def __init__(self, study: Study):
157         self.study = study
158         self.repo = Repository(study)
159         self.base_dir = Path(
160             f"{settings.IMPORT_REPO_PATH}/{self.study}/{settings.IMPORT_SUB_DIRECTORY}/"
161         )
162 
163         self.IMPORT_ORDER = OrderedDict(
164             {
165                 "study": (StudyDescriptionImport, self.base_dir / "study.md"),
166                 "topics.csv": (TopicImport, self.base_dir / "topics.csv"),
167                 "topics.json": (TopicJsonImport, self.base_dir / "topics.json"),
168                 "concepts": (ConceptImport, self.base_dir / "concepts.csv"),
169                 "analysis_units": (
170                     AnalysisUnitImport,
171                     self.base_dir / "analysis_units.csv",
172                 ),
173                 "periods": (PeriodImport, self.base_dir / "periods.csv"),
174                 "conceptual_datasets": (
175                     ConceptualDatasetImport,
176                     self.base_dir / "conceptual_datasets.csv",
177                 ),
178                 "instruments": (
179                     InstrumentImport,
180                     Path(self.base_dir / "instruments/").glob("*.json"),
181                 ),
182                 "datasets.json": (
183                     DatasetJsonImport,
184                     Path(self.base_dir / "datasets/").glob("*.json"),
185                 ),
186                 "datasets.csv": (DatasetImport, self.base_dir / "datasets.csv"),
187                 "variables": (VariableImport, self.base_dir / "variables.csv"),
188                 "questions_variables": (
189                     QuestionVariableImport,
190                     self.base_dir / "questions_variables.csv",
191                 ),
192                 "concepts_questions": (
193                     ConceptQuestionImport,
194                     self.base_dir / "concepts_questions.csv",
195                 ),
196                 "transformations": (
197                     TransformationImport,
198                     self.base_dir / "transformations.csv",
199                 ),
200                 "attachments": (AttachmentImport, self.base_dir / "attachments.csv"),
201                 "publications": (PublicationImport, self.base_dir / "publications.csv"),
202             }
203         )
204 
205     def update_repo(self):
206         self.repo.update_repo()
207         self.repo.set_commit_id()
208 
209     def import_single_entity(self, entity: str, filename: bool = None):
210         """
211         Example usage:
212 
213         study = Study.objects.get(name="some-study")
214         manager = StudyImportManager(study)
215 
216         manager.import_single_entity("study")
217         manager.import_single_entity("periods")
218         manager.import_single_entity("instruments", "instruments/some-instrument.json")
219 
220         """
221         logger.info(f'Study "{self.study.name}" starts import of entity: "{entity}"')
222         importer_class, default_file_path = self.IMPORT_ORDER.get(entity)
223 
224         # import specific file
225         if filename:
226             file = self.base_dir / filename
227             if file.is_file():
228                 logger.info(
229                     f'Study "{self.study.name}" starts import of file: "{file.name}"'
230                 )
231                 importer = importer_class(file, self.study)
232                 django_rq.enqueue(importer.run_import, file, self.study)
233             else:
234                 logger.error(f'Study "{self.study.name}" has no file: "{file.name}"')
235         else:
236 
237             # single file import
238             if isinstance(default_file_path, Path):
239                 if default_file_path.is_file():
240                     importer = importer_class(default_file_path, self.study)
241                     django_rq.enqueue(importer.run_import, default_file_path, self.study)
242                 else:
243                     logger.warning(
244                         f'Study "{self.study.name}" has no file: "{default_file_path.name}"'
245                     )
246 
247             # multiple files import, e.g. instruments, datasets
248             else:
249 
250                 for file in sorted(default_file_path):
251                     logger.info(
252                         f'Study "{self.study.name}" starts import of file: "{file.name}"'
253                     )
254                     importer = importer_class(file, self.study)
255                     django_rq.enqueue(importer.run_import, file, self.study)
256 
257     def import_all_entities(self):
258         """
259         Example usage:
260 
261         study = Study.objects.get(name="some-study")
262         manager = StudyImportManager(study)
263 
264         manager.import_all_entities()
265 
266         """
267         logger.info(f'Study "{self.study.name}" starts importing of all entities')
268         for entity in self.IMPORT_ORDER.keys():
269             self.import_single_entity(entity)
270
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/ddionrails/imports/manager.py b/ddionrails/imports/manager.py
--- a/ddionrails/imports/manager.py
+++ b/ddionrails/imports/manager.py
@@ -139,15 +139,12 @@
         """
         Run the system import.
         """
-
-        base_dir = Path(
-            f"{settings.IMPORT_REPO_PATH}{self.system.name}/{settings.IMPORT_SUB_DIRECTORY}"
-        )
-        studies_file = base_dir / "studies.csv"
-        django_rq.enqueue(StudyImport.run_import, studies_file, self.system)
+        base_directory = self.system.import_path()
+        studies_file = base_directory.joinpath("studies.csv")
+        StudyImport.run_import(studies_file, self.system)
 
         # Copy background image to static/
-        image_file = base_dir / "background.png"
+        image_file = base_directory.joinpath("background.png")
         shutil.copy(image_file, "static/")
         self.repo.set_commit_id()

{"golden_diff": "diff --git a/ddionrails/imports/manager.py b/ddionrails/imports/manager.py\n--- a/ddionrails/imports/manager.py\n+++ b/ddionrails/imports/manager.py\n@@ -139,15 +139,12 @@\n         \"\"\"\n         Run the system import.\n         \"\"\"\n-\n-        base_dir = Path(\n-            f\"{settings.IMPORT_REPO_PATH}{self.system.name}/{settings.IMPORT_SUB_DIRECTORY}\"\n-        )\n-        studies_file = base_dir / \"studies.csv\"\n-        django_rq.enqueue(StudyImport.run_import, studies_file, self.system)\n+        base_directory = self.system.import_path()\n+        studies_file = base_directory.joinpath(\"studies.csv\")\n+        StudyImport.run_import(studies_file, self.system)\n \n         # Copy background image to static/\n-        image_file = base_dir / \"background.png\"\n+        image_file = base_directory.joinpath(\"background.png\")\n         shutil.copy(image_file, \"static/\")\n         self.repo.set_commit_id()\n", "issue": "Run system management command not in queue\n### Subject of the issue\r\n\r\nDo not use the rqworker for the `python manage.py system` command. It runs a single git pull/clone, copies a single and imports a single csv file. This can be done without the rqworker.\r\n\r\nThis is an issue, when starting the web container, which runs  `python manage.py system` before the queue and the rqworker might be ready.\r\n\n", "before_files": [{"content": "import logging\nimport os\nimport re\nimport shutil\nfrom collections import OrderedDict\nfrom pathlib import Path\n\nimport django_rq\nfrom django.conf import settings\n\nfrom config.helpers import script_list, script_list_output\nfrom ddionrails.concepts.imports import (\n    AnalysisUnitImport,\n    ConceptImport,\n    ConceptualDatasetImport,\n    PeriodImport,\n    TopicImport,\n    TopicJsonImport,\n)\nfrom ddionrails.data.imports import (\n    DatasetImport,\n    DatasetJsonImport,\n    TransformationImport,\n    VariableImport,\n)\nfrom ddionrails.instruments.imports import (\n    ConceptQuestionImport,\n    InstrumentImport,\n    QuestionVariableImport,\n)\nfrom ddionrails.publications.imports import AttachmentImport, PublicationImport\nfrom ddionrails.studies.imports import StudyDescriptionImport, StudyImport\nfrom ddionrails.studies.models import Study\n\nlogging.config.fileConfig(\"logging.conf\")\nlogger = logging.getLogger(__name__)\n\n\nclass Repository:\n    def __init__(self, study_or_system):\n        self.study_or_system = study_or_system\n        self.link = study_or_system.repo_url()\n        self.name = study_or_system.name\n        self.path = os.path.join(settings.IMPORT_REPO_PATH, self.name)\n\n    def clone_repo(self):\n        script = [\n            f\"cd {settings.IMPORT_REPO_PATH}\",\n            f\"git clone --depth 1 {self.link} {self.name} -b {settings.IMPORT_BRANCH}\",\n            f\"cd {self.name}\",\n            f\"git checkout -b {settings.IMPORT_BRANCH} origin/{settings.IMPORT_BRANCH}\",\n            f\"git branch --set-upstream-to=origin/{settings.IMPORT_BRANCH} {settings.IMPORT_BRANCH}\",\n        ]\n        script_list(script)\n\n    def set_branch(self, branch=settings.IMPORT_BRANCH):\n        script = [\"cd %s\" % self.path, \"git checkout %s\" % branch]\n        script_list(script)\n\n    def update_repo(self):\n        script = [\"cd %s\" % self.path, \"git pull\"]\n        script_list(script)\n\n    def update_or_clone_repo(self):\n        print(\"[INFO] Repository path: %s\" % self.path)\n        if os.path.isdir(self.path):\n            self.update_repo()\n        else:\n            self.clone_repo()\n\n    def get_commit_id(self):\n        script = [\"cd %s\" % self.path, \"git log --pretty=format:'%H' -n 1\"]\n        return script_list_output(script)\n\n    def set_commit_id(self):\n        study_or_system = self.study_or_system\n        study_or_system.current_commit = self.get_commit_id()\n        study_or_system.save()\n\n    def import_required(self):\n        return self.study_or_system.current_commit != self.get_commit_id()\n\n    def list_changed_files(self):\n        script = [\n            \"cd %s\" % self.path,\n            \"git diff --name-only %s -- %s\"\n            % (self.study_or_system.current_commit, settings.IMPORT_SUB_DIRECTORY),\n        ]\n        return script_list_output(script).split()\n\n    def list_all_files(self):\n        script = [\"cd %s\" % self.path, \"find %s\" % settings.IMPORT_SUB_DIRECTORY]\n        return script_list_output(script).split()\n\n    def import_list(self, import_all=False):\n        if import_all:\n            import_files = self.list_all_files()\n        else:\n            import_files = self.list_changed_files()\n        return import_files\n\n\nclass ImportLink:\n    def __init__(self, expression, importer, activate_import=True):\n\n        print(expression)\n        print(importer)\n        print(activate_import)\n\n        self.expression = re.compile(expression)\n        self.importer = importer\n        self.activate_import = activate_import\n\n    def run_import(self, import_files, study=None):\n        if self.activate_import:\n            for import_file in import_files:\n                self._process_import_file(import_file, study=study)\n\n    def _import(self, study, import_file):\n        self.importer.run_import(import_file, study=study)\n\n    def _process_import_file(self, import_file, study=None):\n        # import_file = import_file.replace(settings.IMPORT_SUB_DIRECTORY, \"\")\n        if self._match(import_file):\n            django_rq.enqueue(self._import, study, import_file)\n\n    def _match(self, import_file):\n        return True if self.expression.match(import_file) else False\n\n\nclass SystemImportManager:\n    \"\"\"Import the files from the system repository.\"\"\"\n\n    def __init__(self, system):\n        self.system = system\n        self.repo = Repository(system)\n\n    def run_import(self):\n        \"\"\"\n        Run the system import.\n        \"\"\"\n\n        base_dir = Path(\n            f\"{settings.IMPORT_REPO_PATH}{self.system.name}/{settings.IMPORT_SUB_DIRECTORY}\"\n        )\n        studies_file = base_dir / \"studies.csv\"\n        django_rq.enqueue(StudyImport.run_import, studies_file, self.system)\n\n        # Copy background image to static/\n        image_file = base_dir / \"background.png\"\n        shutil.copy(image_file, \"static/\")\n        self.repo.set_commit_id()\n\n\nclass StudyImportManager:\n    def __init__(self, study: Study):\n        self.study = study\n        self.repo = Repository(study)\n        self.base_dir = Path(\n            f\"{settings.IMPORT_REPO_PATH}/{self.study}/{settings.IMPORT_SUB_DIRECTORY}/\"\n        )\n\n        self.IMPORT_ORDER = OrderedDict(\n            {\n                \"study\": (StudyDescriptionImport, self.base_dir / \"study.md\"),\n                \"topics.csv\": (TopicImport, self.base_dir / \"topics.csv\"),\n                \"topics.json\": (TopicJsonImport, self.base_dir / \"topics.json\"),\n                \"concepts\": (ConceptImport, self.base_dir / \"concepts.csv\"),\n                \"analysis_units\": (\n                    AnalysisUnitImport,\n                    self.base_dir / \"analysis_units.csv\",\n                ),\n                \"periods\": (PeriodImport, self.base_dir / \"periods.csv\"),\n                \"conceptual_datasets\": (\n                    ConceptualDatasetImport,\n                    self.base_dir / \"conceptual_datasets.csv\",\n                ),\n                \"instruments\": (\n                    InstrumentImport,\n                    Path(self.base_dir / \"instruments/\").glob(\"*.json\"),\n                ),\n                \"datasets.json\": (\n                    DatasetJsonImport,\n                    Path(self.base_dir / \"datasets/\").glob(\"*.json\"),\n                ),\n                \"datasets.csv\": (DatasetImport, self.base_dir / \"datasets.csv\"),\n                \"variables\": (VariableImport, self.base_dir / \"variables.csv\"),\n                \"questions_variables\": (\n                    QuestionVariableImport,\n                    self.base_dir / \"questions_variables.csv\",\n                ),\n                \"concepts_questions\": (\n                    ConceptQuestionImport,\n                    self.base_dir / \"concepts_questions.csv\",\n                ),\n                \"transformations\": (\n                    TransformationImport,\n                    self.base_dir / \"transformations.csv\",\n                ),\n                \"attachments\": (AttachmentImport, self.base_dir / \"attachments.csv\"),\n                \"publications\": (PublicationImport, self.base_dir / \"publications.csv\"),\n            }\n        )\n\n    def update_repo(self):\n        self.repo.update_repo()\n        self.repo.set_commit_id()\n\n    def import_single_entity(self, entity: str, filename: bool = None):\n        \"\"\"\n        Example usage:\n\n        study = Study.objects.get(name=\"some-study\")\n        manager = StudyImportManager(study)\n\n        manager.import_single_entity(\"study\")\n        manager.import_single_entity(\"periods\")\n        manager.import_single_entity(\"instruments\", \"instruments/some-instrument.json\")\n\n        \"\"\"\n        logger.info(f'Study \"{self.study.name}\" starts import of entity: \"{entity}\"')\n        importer_class, default_file_path = self.IMPORT_ORDER.get(entity)\n\n        # import specific file\n        if filename:\n            file = self.base_dir / filename\n            if file.is_file():\n                logger.info(\n                    f'Study \"{self.study.name}\" starts import of file: \"{file.name}\"'\n                )\n                importer = importer_class(file, self.study)\n                django_rq.enqueue(importer.run_import, file, self.study)\n            else:\n                logger.error(f'Study \"{self.study.name}\" has no file: \"{file.name}\"')\n        else:\n\n            # single file import\n            if isinstance(default_file_path, Path):\n                if default_file_path.is_file():\n                    importer = importer_class(default_file_path, self.study)\n                    django_rq.enqueue(importer.run_import, default_file_path, self.study)\n                else:\n                    logger.warning(\n                        f'Study \"{self.study.name}\" has no file: \"{default_file_path.name}\"'\n                    )\n\n            # multiple files import, e.g. instruments, datasets\n            else:\n\n                for file in sorted(default_file_path):\n                    logger.info(\n                        f'Study \"{self.study.name}\" starts import of file: \"{file.name}\"'\n                    )\n                    importer = importer_class(file, self.study)\n                    django_rq.enqueue(importer.run_import, file, self.study)\n\n    def import_all_entities(self):\n        \"\"\"\n        Example usage:\n\n        study = Study.objects.get(name=\"some-study\")\n        manager = StudyImportManager(study)\n\n        manager.import_all_entities()\n\n        \"\"\"\n        logger.info(f'Study \"{self.study.name}\" starts importing of all entities')\n        for entity in self.IMPORT_ORDER.keys():\n            self.import_single_entity(entity)\n", "path": "ddionrails/imports/manager.py"}], "after_files": [{"content": "import logging\nimport os\nimport re\nimport shutil\nfrom collections import OrderedDict\nfrom pathlib import Path\n\nimport django_rq\nfrom django.conf import settings\n\nfrom config.helpers import script_list, script_list_output\nfrom ddionrails.concepts.imports import (\n    AnalysisUnitImport,\n    ConceptImport,\n    ConceptualDatasetImport,\n    PeriodImport,\n    TopicImport,\n    TopicJsonImport,\n)\nfrom ddionrails.data.imports import (\n    DatasetImport,\n    DatasetJsonImport,\n    TransformationImport,\n    VariableImport,\n)\nfrom ddionrails.instruments.imports import (\n    ConceptQuestionImport,\n    InstrumentImport,\n    QuestionVariableImport,\n)\nfrom ddionrails.publications.imports import AttachmentImport, PublicationImport\nfrom ddionrails.studies.imports import StudyDescriptionImport, StudyImport\nfrom ddionrails.studies.models import Study\n\nlogging.config.fileConfig(\"logging.conf\")\nlogger = logging.getLogger(__name__)\n\n\nclass Repository:\n    def __init__(self, study_or_system):\n        self.study_or_system = study_or_system\n        self.link = study_or_system.repo_url()\n        self.name = study_or_system.name\n        self.path = os.path.join(settings.IMPORT_REPO_PATH, self.name)\n\n    def clone_repo(self):\n        script = [\n            f\"cd {settings.IMPORT_REPO_PATH}\",\n            f\"git clone --depth 1 {self.link} {self.name} -b {settings.IMPORT_BRANCH}\",\n            f\"cd {self.name}\",\n            f\"git checkout -b {settings.IMPORT_BRANCH} origin/{settings.IMPORT_BRANCH}\",\n            f\"git branch --set-upstream-to=origin/{settings.IMPORT_BRANCH} {settings.IMPORT_BRANCH}\",\n        ]\n        script_list(script)\n\n    def set_branch(self, branch=settings.IMPORT_BRANCH):\n        script = [\"cd %s\" % self.path, \"git checkout %s\" % branch]\n        script_list(script)\n\n    def update_repo(self):\n        script = [\"cd %s\" % self.path, \"git pull\"]\n        script_list(script)\n\n    def update_or_clone_repo(self):\n        print(\"[INFO] Repository path: %s\" % self.path)\n        if os.path.isdir(self.path):\n            self.update_repo()\n        else:\n            self.clone_repo()\n\n    def get_commit_id(self):\n        script = [\"cd %s\" % self.path, \"git log --pretty=format:'%H' -n 1\"]\n        return script_list_output(script)\n\n    def set_commit_id(self):\n        study_or_system = self.study_or_system\n        study_or_system.current_commit = self.get_commit_id()\n        study_or_system.save()\n\n    def import_required(self):\n        return self.study_or_system.current_commit != self.get_commit_id()\n\n    def list_changed_files(self):\n        script = [\n            \"cd %s\" % self.path,\n            \"git diff --name-only %s -- %s\"\n            % (self.study_or_system.current_commit, settings.IMPORT_SUB_DIRECTORY),\n        ]\n        return script_list_output(script).split()\n\n    def list_all_files(self):\n        script = [\"cd %s\" % self.path, \"find %s\" % settings.IMPORT_SUB_DIRECTORY]\n        return script_list_output(script).split()\n\n    def import_list(self, import_all=False):\n        if import_all:\n            import_files = self.list_all_files()\n        else:\n            import_files = self.list_changed_files()\n        return import_files\n\n\nclass ImportLink:\n    def __init__(self, expression, importer, activate_import=True):\n\n        print(expression)\n        print(importer)\n        print(activate_import)\n\n        self.expression = re.compile(expression)\n        self.importer = importer\n        self.activate_import = activate_import\n\n    def run_import(self, import_files, study=None):\n        if self.activate_import:\n            for import_file in import_files:\n                self._process_import_file(import_file, study=study)\n\n    def _import(self, study, import_file):\n        self.importer.run_import(import_file, study=study)\n\n    def _process_import_file(self, import_file, study=None):\n        # import_file = import_file.replace(settings.IMPORT_SUB_DIRECTORY, \"\")\n        if self._match(import_file):\n            django_rq.enqueue(self._import, study, import_file)\n\n    def _match(self, import_file):\n        return True if self.expression.match(import_file) else False\n\n\nclass SystemImportManager:\n    \"\"\"Import the files from the system repository.\"\"\"\n\n    def __init__(self, system):\n        self.system = system\n        self.repo = Repository(system)\n\n    def run_import(self):\n        \"\"\"\n        Run the system import.\n        \"\"\"\n        base_directory = self.system.import_path()\n        studies_file = base_directory.joinpath(\"studies.csv\")\n        StudyImport.run_import(studies_file, self.system)\n\n        # Copy background image to static/\n        image_file = base_directory.joinpath(\"background.png\")\n        shutil.copy(image_file, \"static/\")\n        self.repo.set_commit_id()\n\n\nclass StudyImportManager:\n    def __init__(self, study: Study):\n        self.study = study\n        self.repo = Repository(study)\n        self.base_dir = Path(\n            f\"{settings.IMPORT_REPO_PATH}/{self.study}/{settings.IMPORT_SUB_DIRECTORY}/\"\n        )\n\n        self.IMPORT_ORDER = OrderedDict(\n            {\n                \"study\": (StudyDescriptionImport, self.base_dir / \"study.md\"),\n                \"topics.csv\": (TopicImport, self.base_dir / \"topics.csv\"),\n                \"topics.json\": (TopicJsonImport, self.base_dir / \"topics.json\"),\n                \"concepts\": (ConceptImport, self.base_dir / \"concepts.csv\"),\n                \"analysis_units\": (\n                    AnalysisUnitImport,\n                    self.base_dir / \"analysis_units.csv\",\n                ),\n                \"periods\": (PeriodImport, self.base_dir / \"periods.csv\"),\n                \"conceptual_datasets\": (\n                    ConceptualDatasetImport,\n                    self.base_dir / \"conceptual_datasets.csv\",\n                ),\n                \"instruments\": (\n                    InstrumentImport,\n                    Path(self.base_dir / \"instruments/\").glob(\"*.json\"),\n                ),\n                \"datasets.json\": (\n                    DatasetJsonImport,\n                    Path(self.base_dir / \"datasets/\").glob(\"*.json\"),\n                ),\n                \"datasets.csv\": (DatasetImport, self.base_dir / \"datasets.csv\"),\n                \"variables\": (VariableImport, self.base_dir / \"variables.csv\"),\n                \"questions_variables\": (\n                    QuestionVariableImport,\n                    self.base_dir / \"questions_variables.csv\",\n                ),\n                \"concepts_questions\": (\n                    ConceptQuestionImport,\n                    self.base_dir / \"concepts_questions.csv\",\n                ),\n                \"transformations\": (\n                    TransformationImport,\n                    self.base_dir / \"transformations.csv\",\n                ),\n                \"attachments\": (AttachmentImport, self.base_dir / \"attachments.csv\"),\n                \"publications\": (PublicationImport, self.base_dir / \"publications.csv\"),\n            }\n        )\n\n    def update_repo(self):\n        self.repo.update_repo()\n        self.repo.set_commit_id()\n\n    def import_single_entity(self, entity: str, filename: bool = None):\n        \"\"\"\n        Example usage:\n\n        study = Study.objects.get(name=\"some-study\")\n        manager = StudyImportManager(study)\n\n        manager.import_single_entity(\"study\")\n        manager.import_single_entity(\"periods\")\n        manager.import_single_entity(\"instruments\", \"instruments/some-instrument.json\")\n\n        \"\"\"\n        logger.info(f'Study \"{self.study.name}\" starts import of entity: \"{entity}\"')\n        importer_class, default_file_path = self.IMPORT_ORDER.get(entity)\n\n        # import specific file\n        if filename:\n            file = self.base_dir / filename\n            if file.is_file():\n                logger.info(\n                    f'Study \"{self.study.name}\" starts import of file: \"{file.name}\"'\n                )\n                importer = importer_class(file, self.study)\n                django_rq.enqueue(importer.run_import, file, self.study)\n            else:\n                logger.error(f'Study \"{self.study.name}\" has no file: \"{file.name}\"')\n        else:\n\n            # single file import\n            if isinstance(default_file_path, Path):\n                if default_file_path.is_file():\n                    importer = importer_class(default_file_path, self.study)\n                    django_rq.enqueue(importer.run_import, default_file_path, self.study)\n                else:\n                    logger.warning(\n                        f'Study \"{self.study.name}\" has no file: \"{default_file_path.name}\"'\n                    )\n\n            # multiple files import, e.g. instruments, datasets\n            else:\n\n                for file in sorted(default_file_path):\n                    logger.info(\n                        f'Study \"{self.study.name}\" starts import of file: \"{file.name}\"'\n                    )\n                    importer = importer_class(file, self.study)\n                    django_rq.enqueue(importer.run_import, file, self.study)\n\n    def import_all_entities(self):\n        \"\"\"\n        Example usage:\n\n        study = Study.objects.get(name=\"some-study\")\n        manager = StudyImportManager(study)\n\n        manager.import_all_entities()\n\n        \"\"\"\n        logger.info(f'Study \"{self.study.name}\" starts importing of all entities')\n        for entity in self.IMPORT_ORDER.keys():\n            self.import_single_entity(entity)\n", "path": "ddionrails/imports/manager.py"}]}