Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Shevchenko committed Mar 26, 2024
0 parents commit b945bcc
Show file tree
Hide file tree
Showing 10 changed files with 638 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
SHELL=/bin/bash

USR ?= /usr/local
INSTALL_DIR_NAME ?= pmt
INSTALL_BIN_NAME ?= $(INSTALL_DIR_NAME)
INSTALL_DIR ?= $(USR)/share/$(INSTALL_DIR_NAME)
INSTALL_BIN_DIR ?= $(USR)/bin
BUILD_DIR ?= ./build

EXECUTABLE_PATH = $(INSTALL_DIR)/main.sh
BIN_EXECUTABLE_PATH = $(INSTALL_BIN_DIR)/$(INSTALL_BIN_NAME)

SOURCE_DIR := ./src

build:
rm -rf $(BUILD_DIR)
cp -r $(SOURCE_DIR) $(BUILD_DIR)

paths_fedora:
cd $(BUILD_DIR) ;\
bash make_fedora_paths_list.sh

watcher:
watcher --clear --cmd "make --always-make build" $(SOURCE_DIR)/*

install:
install -d $(INSTALL_DIR)
cp -r $(SOURCE_DIR)/* $(INSTALL_DIR)
chmod +x $(EXECUTABLE_PATH)
mkdir -p $(INSTALL_BIN_DIR)
ln -s $(EXECUTABLE_PATH) $(BIN_EXECUTABLE_PATH)

uninstall:
rm -r $(INSTALL_DIR)
rm $(BIN_EXECUTABLE_PATH)

clean:
rm -r $(BUILD_DIR)
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# PMT (Package Manager Translator)

PMT is a lightweight command-line tool designed to facilitate the translation of package names between different package managers on Linux distributions, such as apt or dnf. Whether you're migrating projects or managing dependencies across different Linux distributions, PMT aims to simplify the process.

## Features

- **Package Manager Compatibility**: Translate package names between different package managers on Linux distributions seamlessly.
- **Simple Command-Line Interface**: Just install as you would with any distribution.
- **Minimal Dependencies**: Lightweight and efficient, PMT aims to keep dependencies to a minimum.

## Dependencies

- curl
- [pup](https://github.com/ericchiang/pup)
- [gum](https://github.com/charmbracelet/gum)
- [watcher](https://github.com/sethigeet/watcher)(optional, only for development)

Instalation with golang package manager.

```
go install github.com/ericchiang/pup@latest
go install github.com/charmbracelet/gum@latest
```
```
go install github.com/sethigeet/watcher@latest
```

## Build

To build and install PMT, you can use the `make` tool. Use the following commands:

```bash
make
make install # Install globally
make install USR=$HOME/.local # Install only for the current user
```

The `make` command will automatically compile PMT, and the `make install` command will install it according to the specified options.

## Installation

**TODO**

## Usage

### Basic Usage

To translate package names, use the `translate` command followed by the source and target package managers.

```bash
pmt <package-manager> <install-command> <packages...>
```

For example:

```bash
pmt apt install i3-wm bash
```

### Available Package Managers

PMT currently supports the following package managers on Linux distributions:

- [X] apt-get (Debian, Devuan)
- [X] dnf (Fedora)

Package managers for future:

- [ ] apt (Ubuntu repositories)
- [ ] pacman (Arch, Manjaro)
- [ ] apk (Alpine)
- [ ] brew (macOS or Linux)
- [ ] xbps (Void)
- [ ] zypper (zypper)
- [ ] nix-env (Nix)
- [ ] eopkg (Solus)

### Additional Options

- `-h`, `--help`: Display help message and exit.
- `-v`, `--version`: Display version information.
- `-n`, `--dry-run`: Translate without installing.
- `-y`, `--yes`: Enable automatic yes to prompts.

## Examples

**TODO**

## Contribution

Given that the project is under development, expect some translation mistakes. Your contribution to improving PMT is welcome! If you encounter any issues, have feature requests, or wish to contribute enhancements, please don't hesitate to submit an issue or pull request.

## Acknowledgments

I thank Jesus Christ that I have a laptop, time and opportunities to work on this project. And also to everyone who supports or is interested in this project.

---

Thank you for using PMT! If you have any questions or feedback, feel free to reach out. Happy translating! 🚀
108 changes: 108 additions & 0 deletions src/api_fedora.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# TODO: should refactoring
function repo::dnf:fedora:path {

process_node() {
local node="$1"

# cd ..
if [ -z "$node" ]; then
prefix=$(dirname "$prefix")

# directory
elif [[ "${node: -1}" == "/" ]]; then
if [[ "${prefix: -1}" == "/" ]]; then
prefix="$prefix$node"
else
prefix="$prefix/$node"
fi

# file
else
if [[ "${prefix: -1}" != "/" ]]; then
prefix="$prefix/"
fi

echo "$prefix$node"
fi
}

# if has problem when install pkg link then reinstall
try_num=0
update_pkg_link() {
query_page_data=$(curl -s $query_url) # query page
pkg_link=$(echo "$query_page_data" | pup ".stretched-link" attr{href} | head -n 1) # get first result of searching

# TODO: returns warns ONLY when get server error
if [ -z "$pkg_link" ]; then
warn "Problem when download page for information about package, so retry: $target query_url=$query_url "
((try_num++))
if [ "$try_num" -lt 5 ]; then
update_pkg_link
else
error "Problem when try download page for information about package: $target"
fi
fi

found_pkg_name="$(basename "$pkg_link")"
if [ "$found_pkg_name" != "$target" ]; then
error "Package $target is not found in repository"
fi
}

throw_error=true
try_num=0
try_download_data() {
for version in "${__repo_version_fedora_list[@]}"; do

url="$__repo_path_fedora$pkg_link/$version"
data_page=$(curl -s "$url")

# example error 404
successfully_load=$(echo "$data_page" | pup ".container")
if [ -n "$successfully_load" ]; then
throw_error=false
fi

data=$(echo "$data_page" | awk '/<div class="tree">/,/<\/div>/' | sed -e 's/<[^>]*>//g' -e 's/^[ \t]*//;s/[ \t]*$//')

if [ -n "$data" ]; then
break
fi

done

# NOTE: if .container is not exists then has some nginx error from example 404
if [ "$throw_error" = true ]; then
warn "Problem when download page for package, so retry: $target url=$url"
((try_num++))
if [ "$try_num" -lt 5 ]; then
try_download_data
else
error "Cannot to download page for package: $target url=$url"
fi
fi
}

target=$1

query_url="https://packages.fedoraproject.org/search?query=$target"

update_pkg_link

# TODO: in this loop search package in every repo
# in future plans finding connected repository
# in system by dnf
try_download_data

debug "successfully downloaded for package: $target" #if-used: DEBUG

data="${data#"${data%%[![:space:]]*}"}"
data="${data%"${data##*[![:space:]]}"}"

IFS=$'\n'
prefix=""
while IFS= read -r line; do
# Видалення тегів та обробка вузлів
process_node "$line"
done <<< "$data"
}
47 changes: 47 additions & 0 deletions src/api_os.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
declare -A _os_pm_names
_os_pm_names["fedora"]="dnf"
_os_pm_names["debian"]="apt-get"
_os_pm_names["ubuntu"]="apt"
_os_pm_names["devuan"]="apt"

function os::name {
echo $(grep -w ID /etc/os-release | cut -d= -f2 | tr -d '"')
}; __OS_NAME=$(os::name)

function os::pm:name {
local pm_name=${_os_pm_names["$__OS_NAME"]}
if [ -n "$PM" ]; then
echo $PM
elif [ -n "$pm_name" ]; then
echo $pm_name
else
return 1
fi
}; __OS_PM_NAME=$(os::pm:name)


declare -A _os_pm_cmd_install
_os_pm_cmd_install["dnf"]="install"
_os_pm_cmd_install["apt"]="install"
_os_pm_cmd_install["apt-get"]="install"

declare -A _os_pm_cmd_install_prefix
_os_pm_cmd_install_prefix["dnf"]="sudo"
_os_pm_cmd_install_prefix["apt"]="sudo"
_os_pm_cmd_install_prefix["apt-get"]="sudo"
function os::pm:cmd_install {
local prefix_sudo="${_os_pm_cmd_install_prefix[$__OS_PM_NAME]}"
local cmd_install="${_os_pm_cmd_install[$__OS_PM_NAME]}"
if [ -z $cmd_install ]; then
error "Information about package manager $__PS_PM_NAME is not provided"
fi
local result_command="$prefix_sudo $__OS_PM_NAME $cmd_install $@" # Example: sudo apt install i3-wm bash
info "Will execute command: $result_command"
if [ -z "$DRY_RUN" ]; then
if [ -n "$ALWAYS_YES" ]; then
$result_command
else
gum confirm "Execute command that install packages?" && $result_command
fi
fi
}
Loading

0 comments on commit b945bcc

Please sign in to comment.