kaxil commented on a change in pull request #7217: [AIRFLOW-5946] Store source 
code in db
URL: https://github.com/apache/airflow/pull/7217#discussion_r383231226
 
 

 ##########
 File path: airflow/models/dag.py
 ##########
 @@ -1753,6 +1756,43 @@ def get_last_dagrun(self, session=None, 
include_externally_triggered=False):
     def safe_dag_id(self):
         return self.dag_id.replace('.', '__dot__')
 
+    def code(self):
+        if conf.getboolean('core', 'store_code', fallback=False):
+            return self._get_code_from_db(self.fileloc)
+        else:
+            return self._get_code_from_file(self.fileloc)
+
+    def _get_code_from_file(self, fileloc):
+        with self._open_maybe_zipped(fileloc, 'r') as f:
+            code = f.read()
+        return code
+
+    @classmethod
+    def _open_maybe_zipped(cls, f, mode='r'):
+        """
+        Opens the given file. If the path contains a folder with a .zip 
suffix, then
+        the folder is treated as a zip archive, opening the file inside the 
archive.
+
+        :return: a file object, as in `open`, or as in `ZipFile.open`.
+        """
+        ZIP_REGEX = re.compile(r'((.*\.zip){})?(.*)'.format(re.escape(os.sep)))
+        _, archive, filename = ZIP_REGEX.search(f).groups()
+        if archive and zipfile.is_zipfile(archive):
+            return zipfile.ZipFile(archive, mode=mode).open(filename)
+        else:
+            return io.open(f, mode=mode)
+
+    @provide_session
+    def _get_code_from_db(self, session=None):
+        fileloc_hash = DagCodeModel.dag_fileloc_hash(fileloc)
 
 Review comment:
   ```suggestion
           fileloc_hash = DagCode.dag_fileloc_hash(fileloc)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to