Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions litebird_sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from .mpi import MPI_COMM_WORLD, MPI_ENABLED, MPI_CONFIGURATION
from .observations import Observation
from .simulations import Simulation
from .version import __author__, __version__

__author__ = "The LiteBIRD simulation team"
__version__ = "0.1.0"
__all__ = [
"__author__",
"__version__",
# healpix.py
"nside_to_npix",
"npix_to_nside",
Expand Down
104 changes: 101 additions & 3 deletions litebird_sim/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import codecs
from collections import namedtuple
from datetime import datetime
import logging as log
import subprocess
from typing import List, Tuple, Any
from pathlib import Path
from shutil import copyfile, copytree
Expand All @@ -10,6 +13,10 @@
from .distribute import distribute_evenly, distribute_optimally
from .healpix import write_healpix_map_to_file
from .observations import Observation
from .version import (
__version__ as litebird_sim_version,
__author__ as litebird_sim_author,
)

from astropy.time import Time, TimeDelta
import markdown
Expand Down Expand Up @@ -87,11 +94,83 @@ def __init__(

self.list_of_outputs = [] # type: List[OutputFileRecord]

self.report = f"""# {name}
self.report = """# {name}

{description}

"""
""".format(
name=name if (name and (name != "")) else "<Untitled>",
description=description,
)

def write_code_status_to_report(self):
self.append_to_report(
"""# Source code used in the simulation

- Main repository: [github.com/litebird/litebird_sim](https://github.com/litebird/litebird_sim)
- Version: {litebird_sim_version}, by {litebird_sim_author}
""".format(
litebird_sim_version=litebird_sim_version,
litebird_sim_author=litebird_sim_author,
)
)

# Retrieve information about the last git commit
try:
proc = subprocess.run(
["git", "log", "-1", "--format=format:%h%n%H%n%s%n%an"],
capture_output=True,
encoding="utf-8",
)

(
short_commit_hash,
commit_hash,
commit_message,
author,
) = proc.stdout.strip().split("\n")

self.append_to_report(
"""
- Commit hash: [{short_commit_hash}](https://github.com/litebird/litebird_sim/commit/{commit_hash})
(_{commit_message}_, by {author})

""".format(
short_commit_hash=short_commit_hash,
commit_hash=commit_hash,
author=author,
commit_message=commit_message,
)
)

# Retrieve information about changes in the code since the last commit
proc = subprocess.run(
["git", "diff", "--no-color", "--exit-code"],
capture_output=True,
encoding="utf-8",
)

if proc.returncode != 0:
self.append_to_report(
"""Since the last commit, the following changes have been made to the
code that has been ran:

```
{0}
```

""".format(
proc.stdout.strip(),
)
)

except FileNotFoundError:
# Git is not installed, so ignore the error and continue
pass
except Exception as e:
log.warning(
f"unable to save information about latest git commit in the report: {e}"
)

def write_healpix_map(self, filename: str, pixels, **kwargs,) -> str:
"""Save a Healpix map in the output folder
Expand Down Expand Up @@ -191,14 +270,33 @@ def flush(self):

"""

# Append to the repository a snapshot containing the status of
# the source code
self.write_code_status_to_report()

self.append_to_report(
"""---

Report written on {datetime}
""".format(
datetime=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
)

# Expand the markdown text using Jinja2
with codecs.open(self.base_path / "report.md", "w", encoding="utf-8") as outf:
outf.write(self.report)

# Now generate an HTML file from Markdown.

# Please keep these in alphabetic order, so we can detect duplicates!
md_extensions = [KatexExtension(), "sane_lists", "smarty", "tables"]
md_extensions = [
KatexExtension(),
"fenced_code",
"sane_lists",
"smarty",
"tables",
]
html = markdown.markdown(self.report, extensions=md_extensions)

static_path = Path(__file__).parent / ".." / "static"
Expand Down
4 changes: 4 additions & 0 deletions litebird_sim/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-

__author__ = "The LiteBIRD simulation team"
__version__ = "0.1.0"