added Parch Profiler

This commit is contained in:
Sohrab Behdani 2024-08-20 19:02:31 +03:30
parent f807a443e9
commit 02a45defc0
2 changed files with 301 additions and 58 deletions

182
README.md
View file

@ -1,93 +1,159 @@
# parch-profiler
### 📦 **Parch-Profiler**
Parch-Profiler is a powerful Python-based utility designed specifically for Parch Linux. It simplifies the management of system packages and configurations by allowing you to generate, validate, and install packages—including AUR and Flatpak—using a structured TOML configuration file.
## Getting started
---
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
## 🚀 **Features**
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
- **Package Installation:**
- Install packages using Pacman, AUR (via `paru`), and Flatpak from a single TOML file.
## Add your files
- **Configuration Management:**
- Clone configuration files, such as Neovim setups, directly from Git repositories.
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
- **Systemd Service Management:**
- Enable and start systemd services as specified in the TOML configuration.
```
cd existing_repo
git remote add origin https://git.parchlinux.com/applications/parch-profiler.git
git branch -M main
git push -uf origin main
- **TOML Validation:**
- Validate your TOML files to ensure they are correctly formatted.
- **Package List Generation:**
- Automatically generate TOML files listing all installed Pacman, AUR, and Flatpak packages.
- **File-based TOML Generation:**
- Convert simple text files listing packages into structured TOML files.
---
## 📄 **Usage**
### **1. Loading a TOML Configuration**
To install packages, clone configurations, and enable services:
```bash
python parch-profiler.py --load config.toml
```
## Integrate with your tools
### **2. Validating a TOML Configuration**
- [ ] [Set up project integrations](https://git.parchlinux.com/applications/parch-profiler/-/settings/integrations)
Check if your TOML file is correctly formatted:
## Collaborate with your team
```bash
python parch-profiler.py --check config.toml
```
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
### **3. Generating a System Package List**
## Test and Deploy
Generate a TOML file listing all installed Pacman, AUR, and Flatpak packages:
Use the built-in continuous integration in GitLab.
```bash
python parch-profiler.py --generate-system output.toml
```
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
### **4. Generating a TOML from a Text File**
***
Convert a simple text file into a TOML configuration:
# Editing this README
```bash
python parch-profiler.py --generate-file input.txt output.toml
```
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
### **5. Reviewing and Editing Generated TOML Files**
## Suggestions for a good README
After generating a TOML file, you will be prompted to review and edit it using your preferred text editor (`vim` or `nano`).
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
---
## Name
Choose a self-explaining name for your project.
## 🧰 **Sample `proto.toml` File**
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
Heres a sample `proto.toml` file that you can use as a starting point for your configurations:
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
```toml
# Pacman packages to be installed
[packages]
packages = [
"base-devel",
"neovim",
"git"
]
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
# AUR packages to be installed
[aur]
aur_packages = [
"google-chrome",
"paru-bin"
]
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
# Flatpak packages to be installed
[flatpak]
flatpak_packages = [
"com.spotify.Client",
"org.videolan.VLC"
]
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
# Configuration repositories to clone
[config]
nvim = { url = "https://gitlab.com/yourusername/nvim-config.git" }
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
# Systemd services to enable and start
[systemd]
systemd_services = [
"docker.service",
"nginx.service"
]
```
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
---
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
## 📄 **Sample `input.txt` for TOML Generation**
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
If you prefer to start from a simple text file, heres how your `input.txt` might look:
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
```text
packages: base-devel, neovim, git
aur: google-chrome, paru-bin
flatpak: com.spotify.Client, org.videolan.VLC
```
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
---
## 🛠️ **Installation**
To use Parch-Profiler on Parch Linux, clone the repository and ensure you have Python installed on your system:
```bash
git clone https://gitlab.com/yourusername/parch-profiler.git
cd parch-profiler
python parch-profiler.py --help
```
---
## ✨ **Contributing**
We welcome contributions! Please feel free to submit a Merge Request or open an Issue on GitLab.
---
## 📜 **License**
This project is licensed under the GPL-3 License - see the [LICENSE](./LICENSE) file for details.
---
## 🛠️ **Requirements**
- **Python 3.x**
- **Parch Linux**
- **Pacman, paru, flatpak**
---
## 📧 **Support**
For any issues or feature requests, please reach out through the GitLab repository.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.

177
profiler.py Normal file
View file

@ -0,0 +1,177 @@
import toml
import subprocess
import os
import argparse
def run_subprocess(command, verbose=False):
try:
if verbose:
result = subprocess.run(command)
else:
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
print(f"Error running command: {' '.join(command)}")
return result
except subprocess.CalledProcessError as e:
print(f"Subprocess error: {e}")
def install_generic_packages(command, packages):
for package in packages:
print(f"Installing package: {package}")
run_subprocess(command + [package])
def install_packages(packages):
install_generic_packages(["sudo", "pacman", "-S", "--noconfirm"], packages)
def install_aur_packages(aur_packages):
install_generic_packages(["paru", "-S", "--noconfirm"], aur_packages)
def install_flatpak_packages(flatpak_packages):
install_generic_packages(["flatpak", "install", "-y"], flatpak_packages)
def clone_config(config):
for app, details in config.items():
if app == "nvim":
url = details['url']
target_dir = os.path.expanduser("~/.config/nvim")
print(f"Cloning Neovim config from {url} to {target_dir}")
run_subprocess(["git", "clone", url, target_dir])
def enable_systemd_services(services):
for service in services:
print(f"Enabling systemd service: {service}")
run_subprocess(["sudo", "systemctl", "enable", service])
run_subprocess(["sudo", "systemctl", "start", service])
def check_toml(toml_file):
try:
toml.load(toml_file)
print(f"{toml_file} is valid.")
except toml.TomlDecodeError as e:
print(f"Error in {toml_file}: {e}")
def generate_system_toml(output_file):
# Get Pacman packages
pacman_packages = subprocess.check_output(["pacman", "-Qeq"], text=True).splitlines()
# Get AUR packages (using paru or yay, assuming paru here)
aur_packages = subprocess.check_output(["paru", "-Qmq"], text=True).splitlines()
# Get Flatpak packages
flatpak_packages = subprocess.check_output(["flatpak", "list", "--app", "--columns=application"], text=True).splitlines()
# Create TOML structure
config = {
"packages": {"packages": pacman_packages},
"aur": {"aur_packages": aur_packages},
"flatpak": {"flatpak_packages": flatpak_packages},
}
# Write to TOML file
with open(output_file, "w") as f:
toml.dump(config, f)
print(f"Generated system package list in {output_file}")
# Ask user to review and edit the file
review = input("Do you want to review and edit the file? (y/n): ").strip().lower()
if review == "y":
editor = input("Choose an editor (vim/nano): ").strip().lower()
if editor not in ["vim", "nano"]:
print("Invalid editor choice. Skipping review.")
else:
subprocess.run([editor, output_file])
print(f"TOML file saved as {output_file}")
def generate_file_toml(input_file, output_file):
config = {
"packages": {"packages": []},
"aur": {"aur_packages": []},
"flatpak": {"flatpak_packages": []},
}
with open(input_file, "r") as f:
for line in f:
key, value = line.split(":")
key = key.strip()
packages = [pkg.strip() for pkg in value.split(",")]
if key == "packages":
config["packages"]["packages"].extend(packages)
elif key == "aur":
config["aur"]["aur_packages"].extend(packages)
elif key == "flatpak":
config["flatpak"]["flatpak_packages"].extend(packages)
with open(output_file, "w") as f:
toml.dump(config, f)
print(f"Generated TOML file from {input_file} and saved as {output_file}")
# Ask user to review and edit the file
review = input("Do you want to review and edit the file? (y/n): ").strip().lower()
if review == "y":
editor = input("Choose an editor (vim/nano): ").strip().lower()
if editor not in ["vim", "nano"]:
print("Invalid editor choice. Skipping review.")
else:
subprocess.run([editor, output_file])
print(f"TOML file saved as {output_file}")
def main():
parser = argparse.ArgumentParser(description="Install packages and manage configurations from a TOML file.")
parser.add_argument('--load', type=str, help='Path to the TOML file to load and install packages.')
parser.add_argument('--check', type=str, help='Path to the TOML file to check for errors.')
parser.add_argument('--generate-system', type=str, help='Generate a TOML file with system installed packages.')
parser.add_argument('--generate-file', type=str, nargs=2, metavar=('input', 'output'),
help='Generate a TOML file from a text file. Provide input and output file paths.')
args = parser.parse_args()
if args.load:
toml_file = args.load
if os.path.exists(toml_file) and os.path.isfile(toml_file):
config = toml.load(toml_file)
else:
print(f"TOML file {toml_file} does not exist or is not a valid file.")
return
# Install packages
if 'packages' in config:
install_packages(config['packages']['packages'])
# Install AUR packages
if 'aur' in config:
install_aur_packages(config['aur']['aur_packages'])
# Install Flatpak packages
if 'flatpak' in config:
install_flatpak_packages(config['flatpak']['flatpak_packages'])
# Clone configuration
if 'config' in config:
clone_config(config['config'])
# Enable systemd services
if 'systemd' in config:
enable_systemd_services(config['systemd']['systemd_services'])
elif args.check:
toml_file = args.check
check_toml(toml_file)
elif args.generate_system:
output_file = args.generate_system
generate_system_toml(output_file)
elif args.generate_file:
input_file, output_file = args.generate_file
generate_file_toml(input_file, output_file)
else:
print("Please provide a valid argument.")
if __name__ == "__main__":
main()